Java Articles

Page 69 of 450

What is overloading? What happens if we overload a main method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 7K+ Views

Overloading is one of the mechanisms to achieve polymorphism. If a class contains two methods with the same name and different parameters, whenever you call this method the method body will be bound with the method call based on the parameters.ExampleIn the following Java program, the Calculator class has two methods with name addition. The only difference between them is that one contains 3 parameters and the other contains 2 parameters.Here, we can call the addition method by passing two integers or three integers. Based on the number of integer values we pass, the respective method will be executed.public class ...

Read More

What are unreachable blocks in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 592 Views

A code block/statement in Java to which the control never reaches and never gets executed during the lifetime of the program is known as unreachable block/statement.Generally, it happens whenever a piece of code hasA return statement before it.An infinite loop before it.Java does not support unreachable code. If you have any such statements, (which are unreachable) Java compiler raises a compile time error.Example1In the following Java program, the class UnreachableCodeExample has a method named greet which returns a String value. After the return statement it has a print statement.public class UnreachableCodeExample {    public String greet() {       ...

Read More

While working with Java in eclipse I got a warning saying "dead code". What does it mean?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

A code block/statement in Java to which the control never reaches and never gets executed during the lifetime of the program is known as unreachable block/statement.public class UnreachableCodeExample {    public String greet() {       System.out.println("This is a greet method ");       return "Hello";       System.out.println("This is an unreachable code ");    }    public static void main(String args[]) {       new UnreachableCodeExample().greet();    } }Dead codeA dead code is an unreachable code, but it doesn’t generate compile time error. But if you execute it in eclipse it gives you a warning.ExampleIn ...

Read More

Can we define an abstract class without abstract method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 6K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Abstract classA class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.And yes, you can declare abstract class without defining an abstract method in it. Once you declare a class abstract it indicates that the class is incomplete ...

Read More

Can we declare an abstract method final or static in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 12K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Declaring abstract method staticIf you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.If you still, try to declare an abstract method static a compile time error is generated saying ...

Read More

Can we declare an abstract method, private, protected, public or default in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 12K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Declaring an abstract method privateIf a method of a class is private, you cannot access it outside the current class, not even from the child classes of it.But, incase of an abstract method, you cannot use it from the same class, you need to override it from subclass and use.Therefore, the abstract method cannot ...

Read More

How to rethrow an exception in Java?

raja
raja
Updated on 11-Mar-2026 19K+ Views

Sometimes we may need to rethrow an exception in Java. If a catch block cannot handle the particular exception it has caught, we can rethrow the exception. The rethrow expression causes the originally thrown object to be rethrown.Because the exception has already been caught at the scope in which the rethrow expression occurs, it is rethrown out to the next enclosing try block. Therefore, it cannot be handled by catch blocks at the scope in which the rethrow expression occurred. Any catch blocks for the enclosing try block have an opportunity to catch the exception.Syntaxcatch(Exception e) {    System.out.println("An exception ...

Read More

Can a constructor throw an exception in Java?

raja
raja
Updated on 11-Mar-2026 6K+ Views

Yes, constructors are allowed to throw an exception in Java.A Constructor is a special type of a method that is used to initialize the object and it is used to create an object of a class using the new keyword, where an object is also known as an Instance of a class. Each object of a class will have its own state (Instance variables) and access to methods of its class.Throw an Exception from a ConstructorA checked exception can be used to indicate a legitimate problem when trying to create an instance, while an unchecked exception typically indicates a bug either in the ...

Read More

What is the importance of "Java.lang.Class" in Java?

raja
raja
Updated on 11-Mar-2026 510 Views

The java.lang.Class is one of the most important class in Java and it can provide several utility methods like getClass(), forName() which is used to find and load a class. It can also provide methods like Class.newInstance() which is the backbone of reflection and allow us to create an instance of a class without using new() operator.Importance of java.lang.ClassInstances of the class Class represent classes, interfaces,  enum and annotation in a running Java application.Whenever a java file is compiled, the compiler will insert a public, static, final field named Class of the type java.lang.Class into generated .class fileEach and every class exposes its code in the form of an ...

Read More

Can a method local inner class access the local final variables in Java?

raja
raja
Updated on 11-Mar-2026 904 Views

Yes, we can access the local final variables using the method local inner class because the final variables are stored on the heap and live as long as the method local inner class object may live.Method Local Inner ClassA local inner class instance can be delivered as an argument and retrieved from the methods and it is available inside a valid scope.The only limitation in method local inner class is that a local parameter can be executed only when it is defined as final.The method executing the local parameters can be called after the execution of the method, within which the local inner ...

Read More
Showing 681–690 of 4,498 articles
« Prev 1 67 68 69 70 71 450 Next »
Advertisements