Static and Final Flashcards
In Java, why do we use static variable?
Whenever we want to have a common property for all objects of a class, we use a class level variable i.e. a static variable. This variable is loaded in memory only once at the time of class loading. So it saves memory, since it is not defined per object in Java.
Why it is not a good practice to create static variables in Java?
Static variables are common to all the objects of a class. If a new object is created, there is no need to test the value of static variable.
Any code that uses static variable can be in any state. It can be within a new object or at a class level. So the scope of static variable is open ended in a Java class. If we want tighter control on scope, then variables should be created at the object creation level. Also defining static variables is not a good practice because they go against the principles of Object Oriented Programming.
What is the purpose of static method in Java?
Java provides the feature of static method to create behavior at the class level.
Why do we mark main method as static in Java?
The main method in Java is marked as static, so that JVM can call it to start the program. If main method is not static, then which constructor will be called by Java process?
What is a static block?
In a Java class, a static block is a set of instructions that is run only once when a class is loaded into memory. A static block is also called a static initialization block.
In what scenario do we use a static block?
At times, there is a class that has static member variables. These variables need some complicated initialization. At this time static block helps as a tool to initialize complex static member variable initialization.
The static block is executed even before the execution of main. Sometimes, we can also replace static block with a static method of
class.
Is it possible to execute a program without defining a main() method?
No, with Java 7 onwards, you need a main() method to execute a
program. In earlier versions of Java, there was a workaround
available to use static blocks for execution. But now this gap has
been closed.
What happens when static modifier is not mentioned in the signature of main method?
As per Java specification, main method has to be marked as static. It needs only one argument that is an array of String.
A program can compile with a non-static method. But on execution
it will give NoSuchMethodError.
Why Integer class in final in Java?
Integer class is a wrapper for int. If it is not marked final, then any other class can extend it and modify the behavior of Integer operations. To avoid this Integer wrapper class is marked as final.
What is a blank final variable in Java?
When we declare a final variable without giving any initial value, then it is called blank final variable.
How can we initialize a blank final variable?
A blank final instance variable can be initialized in a constructor.
A blank final static variable can be initialized in the static block of class.