Found 2616 Articles for Java

Can we declare the main () method as final in Java?

raja
Updated on 06-Feb-2020 11:11:06

4K+ Views

Yes, we can declare the main () method as final in Java. The compiler does not throw any error.If we declare any method as final by placing the final keyword then that method becomes the final method.The main use of the final method in Java is they are not overridden.We can not override final methods in subclasses.If we are using inheritance and we need some methods not to overridden in subclasses then we need to make it final so that those methods can't be overridden by subclasses.We can access final methods in the subclass but we can not override final methods.Exampleclass BaseClass ... Read More

Why the main () method in Java is always static?

raja
Updated on 30-Jul-2019 22:30:26

15K+ Views

Java main() method is always static, so that compiler can call it without the creation of an object or before the creation of an object of the class.In any Java program, the main() method is the starting point from where compiler starts program execution. So, the compiler needs to call the main() method.If the main() is allowed to be non-static, then while calling the main() method JVM has to instantiate its class.While instantiating it has to call the constructor of that class, There will be ambiguity if the constructor of that class takes an argument.Static method of a class can be ... Read More

Can we define an abstract class with no abstract methods in Java?

raja
Updated on 22-Nov-2023 09:15:56

9K+ Views

Yes, we can declare an abstract class with no abstract methods in Java. An abstract class means that hiding the implementation and showing the function definition to the user. An abstract class having both abstract methods and non-abstract methods. For an abstract class, we are not able to create an object directly. But Indirectly we can create an object using the subclass object. A Java abstract class can have instance methods that implement a default behavior. An abstract class can extend only one class or one abstract class at a time. Declaring a class as abstract with no abstract methods means that we don't allow it ... Read More

Why an interface doesn't have a constructor whereas an abstract class have a constructor in Java?

raja
Updated on 22-Nov-2023 09:23:10

8K+ Views

A Constructor is to initialize the non-static members of a particular class with respect to an object. Constructor in an Interface An Interface in Java doesn't have a constructor because all data members in interfaces are public static final by default, they are constants (assign the values at the time of declaration). There are no data members in an interface to initialize them through the constructor. In order to call a method, we need an object, since the methods in the interface don’t have a body there is no need for calling the methods in an interface. Since we cannot call the methods in the ... Read More

Why an interface cannot implement another interface in Java?

raja
Updated on 22-Nov-2023 09:29:18

10K+ Views

An interface cannot implement another interface in Java. An interface in Java is essentially a special kind of class. Like classes, the interface contains methods and variables. Unlike classes, interfaces are always completely abstract. An interface is defined just like a class except for the keyword interface in place of a class, the variables are declared in an interface are static and final and the methods are defined in an interface are public abstract methods. An interface can extend any number of interfaces but one interface cannot implement another interface, because if any interface is implemented then its methods must be defined and ... Read More

What are the steps to read static members in a Java class?

raja
Updated on 22-Nov-2023 09:34:23

302 Views

A static variable gets created at the time of class loading even before the execution of a static block and the purpose of the static block is to assign value to the static variables. A static variable stores a value that is shared between all instances of the class it is defined in and a static block is a section of code that gets executed when class is first loaded. If we want any logic that needs to be executed at the time of class loading that logic needs to place inside the static block so that it will be executed ... Read More

How to handle the Runtime Exception in Java?

raja
Updated on 06-Feb-2020 10:36:17

12K+ Views

The Runtime Exception is the parent class in all exceptions of the Java programming language that are expected to crash or break down the program or application when they occur. Unlike exceptions that are not considered as Runtime Exceptions, Runtime Exceptions are never checked.The Runtime Exception usually shows the programmer's error, rather than the condition a program is expected to deal with. Runtime Exceptions are also used when a condition that can't happen. It should be noted that when a program is running out of memory, a program error is thrown instead of showing it as a Runtime Exception.The most ... Read More

How to solve an IllegalArgumentException in Java?

raja
Updated on 28-Nov-2023 10:26:54

26K+ Views

An IllegalArgumentException is thrown in order to indicate that a method has been passed an illegal argument. This exception extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). It is an unchecked exception and thus, it does not need to be declared in a method’s or a constructor’s throws clause. Reasons for java.lang.IllegalArgumentException When Arguments out of range. For example, the percentage should lie between 1 to 100. If the user entered 101 then an IllegalArugmentExcpetion will be thrown. When argument format is invalid. For example, if our method ... Read More

What are the differences between while loop and do-while loop in Java?

raja
Updated on 21-Nov-2023 14:41:27

8K+ Views

The while loop in java executes one or more statements after testing the loop continuation condition at the start of each iteration. The do-while loop, however, tests the loop continuation condition after the first iteration has completed. Therefore, the do-while loop guarantees one execution of the loop logic whereas the while does not. Example public class WhileAndDoWhileLoop { public static void main(String args[]) { int i=5; System.out.println("Test while Loop:"); while(i < 5) { System.out.println("Iteration: "+ ++i); ... Read More

What is Double-buffering in Java?

raja
Updated on 30-Jul-2019 22:30:26

2K+ Views

Double-buffering is the process of drawing graphics into an off-screen image buffer and then copying the contents of the buffer to the screen all at once.For the complex graphics, using double-buffering can reduce flickering issues.Java Swing automatically supports double-buffering for all of its components.Double-buffering is memory intensive, its use is only justified for components that are repainted very frequently or have particularly complex graphics to display.If a container uses double-buffering, any double-buffered children it has shared the off-screen buffer of the container, the required off-screen buffer is never larger than the on-screen size of the application.To enable double buffering, simply ... Read More

Advertisements