Java Core: Static Flashcards

1
Q

What is a static variable? When to use them?

A

A static variable is a field/variable that belongs to an entire class rather than a single object. Memory for this variable is allocated in a stack. The can be used when a class should have a shared variable for all its objects.

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

What is a static method? When to use them?

A

A static method is a method that belongs to an entire class rather than a single object of this class. These methods can be used when a class should have a method that exists independently of any object.

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

What are the static initialization blocks? When to use them?

A

Code inside static initialization blocks is executed only once - when the class (but not its objects) is first loaded. Usually, they can be used when needed to initialize static variables, but determining the values requires some operations.

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

What are static inner classes? How are they different from non-static inner classes?

A

Static inner classes are nested classes that have been defined as static. Unlike non-static, we can use static nested classes without initializing an instance of the outer class. It makes them more practical if you wish to use the nested class several times without using the outer class.

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

Why is the ‘Main’ method always static in Java?

A

The ‘main’ method is the program’s entry point when no objects have been created yet, It is always static because JVM should be able to call it before any objects have been created.

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