Static and Final Flashcards

1
Q

In Java, why do we use static variable?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why it is not a good practice to create static variables in Java?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of static method in Java?

A

Java provides the feature of static method to create behavior at the class level.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why do we mark main method as static in Java?

A

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?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a static block?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In what scenario do we use a static block?

A
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is it possible to execute a program without defining a main() method?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens when static modifier is not mentioned in the signature of main method?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why Integer class in final in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a blank final variable in Java?

A

When we declare a final variable without giving any initial value, then it is called blank final variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can we initialize a blank final variable?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly