Object Oriented Programming Articles

Page 253 of 589

Can we declare final variables without initialization in java?

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

In Java, final is the access modifier which can be used with a filed class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is final it cannot be extended.Declaring final variable without initializationIf you declare a final variable later on you cannot modify or, assign values to it. Moreover, like instance variables, final variables will not be initialized with default values.Therefore, it is mandatory to initialize final variables once you declare them.Still, if you try to declare final variables without initialization that will generate a ...

Read More

Why main() method must be static in java?

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

Static − If you declare a method, subclass, block, or a variable static it is loaded along with the class.In Java whenever we need to call an (instance) method we should instantiate the class (containing it) and call it. If we need to call a method without instantiation it should be static. Moreover, static methods are loaded into the memory along with the class.In the case of the main method, it is invoked by the JVM directly, so it is not possible to call it by instantiating its class. And, it should be loaded into the memory along with the ...

Read More

Can we declare main() method as private or protected or with no access modifier in java?

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

Java provides various access specifiers namely private, public and protected etc...The Private modifier restricts the access of members from outside the class. A class and interface cannot be public.The Public access modifier can be associated with class, method, constructor, interface, etc. public can be accessed from any other class.The protected access modifier can be associated with variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.The default access modifier does not have keyword a variable or method declared ...

Read More

Can We declare main() method as Non-Static in java?

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

The public static void main(String ar[]) method is the entry point of the execution in Java. When we run a .class file JVM searches for the main method and executes the contents of it line by line.You can write the main method in your program without the static modifier, the program gets compiled without compilation errors. But, at the time of execution JVM does not consider this new method (without static) as the entry point of the program.  It searches for the main method which is public, static, with return type void, and a String array as an argument.public static int main(String[] ...

Read More

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 582 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

How many ways are there to initialize a final variable in java?

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

In Java, final is the access modifier which can be used with a filed, class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is final it cannot be extended.Initializing a final variableOnce you declare a final variable, it is a must to initialize it. You can initialize the final instance variable −At the time of declaration as.public final String name = "Raju"; public final int age = 20;Within an instance (non-static) block.{    this.name = "Raju";    this.age = 20; }Within a default constructor.public final ...

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

Why final variable doesn't require initialization in main method in java?

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

In Java final is the access modifier which can be used with a filed class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is final it cannot be extended.Declaring final variable without initializationIf you declare a variable as final, it is mandatory to initialize it before the end of the constructor. If you don’t, a compile-time error is generated.Examplepublic class Student {    public final String name;    public final int age;    public void display(){       System.out.println("Name of the Student: "+this.name);   ...

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
Showing 2521–2530 of 5,881 articles
« Prev 1 251 252 253 254 255 589 Next »
Advertisements