Static Flashcards

(113 cards)

1
Q

Why do we use static variables in Java?

A

Static variables store values shared by all objects of a class, so only one copy exists regardless of how many objects are created.

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

What is a static variable in Java?

A

A static variable is a class-level variable declared with static that belongs to the class rather than instances.

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

When is a static variable created in memory?

A

It is created once when the class is loaded by the JVM.

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

Why do static variables save memory?

A

Because only one copy exists instead of separate copies for each object.

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

How do you access a static variable?

A

Using ClassName.variableName (preferred) or through an object reference.

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

Example of static variable declaration?

A

static int count;

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

Can static variables be accessed without creating objects?

A

Yes. Static members belong to the class and can be accessed directly via class name.

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

Where are static variables stored in JVM memory?

A

In the Method Area (class metadata area).

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

Can static variables be overridden?

A

No. They can be hidden but not overridden.

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

What is variable hiding for static variables?

A

It occurs when a subclass defines a static variable with the same name as in superclass.

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

Can static variables be final?

A

Yes. static final variables are constants.

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

Why are static variables useful for counters?

A

Because all objects share the same value, allowing global tracking.

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

What is a real-world example use of static variables?

A

A counter tracking number of created objects or a shared configuration value.

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

When should you avoid static variables?

A

When data should be instance-specific or when shared mutable state may cause concurrency issues.

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

Why is excessive use of static variables considered bad practice?

A

Because they create shared global state, which increases coupling, reduces flexibility, and makes code harder to test and maintain.

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

What is the main design problem with static variables?

A

They introduce global mutable state that any part of the program can modify.

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

How do static variables affect object-oriented design?

A

They break encapsulation and object independence because all objects share the same value.

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

Why can static variables make debugging difficult?

A

Because any code anywhere can change them, making state changes hard to trace.

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

How do static variables affect testing?

A

They make unit tests unreliable because shared state can leak between tests.

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

What concurrency issue can static variables cause?

A

Race conditions when multiple threads modify shared static data.

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

When are static variables appropriate to use?

A

When data truly belongs to the class, such as constants or shared configuration.

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

What is safer than mutable static variables?

A

static final constants.

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

Why are static variables considered global variables?

A

Because they are accessible across all instances and often across the entire application.

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

Do static variables violate OOP principles?

A

Overuse can violate encapsulation and modularity principles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Why is instance state usually preferred over static state?
Instance state keeps data isolated per object, improving modularity and predictability.
26
What is a better design alternative to static shared state?
Dependency injection or passing state explicitly between objects.
27
When do static variables become especially risky?
In multithreaded or large-scale applications.
28
Can static variables be thread-safe?
Yes, but only with proper synchronization or immutable design.
29
What is the key rule for using static variables wisely?
Use them only when the data logically belongs to the class and must be shared across all instances.
30
What is the purpose of static methods in Java?
Static methods define behavior that belongs to the class rather than individual objects and can be called without creating an instance.
31
What is a static method in Java?
A static method is a method declared with static that belongs to the class instead of an object.
32
How do you call a static method?
Using ClassName.methodName().
33
Example of static method declaration?
static void display() {}
34
Why don’t static methods need objects?
Because they operate at class level and are loaded when the class is loaded.
35
What can static methods access directly?
Only static variables and other static methods.
36
Can static methods access instance variables?
No, unless they use an object reference.
37
Why can’t static methods access instance members directly?
Because instance members belong to objects, and static methods do not have an object context.
38
Can static methods be overridden?
No. They can be hidden but not overridden.
39
What is method hiding?
It occurs when a subclass defines a static method with the same signature as a static method in the superclass.
40
When should you use static methods?
When behavior does not depend on object state.
41
Example real-world use of static methods?
Utility classes like Math.sqrt().
42
Can static methods use this or super?
No, because they do not operate on an instance.
43
What is a common design use for static methods?
Utility/helper methods or factory methods.
44
Why are static methods memory efficient?
Because only one copy exists regardless of number of objects.
45
Why is the main method declared static in Java?
Because JVM must call main without creating an object, and static methods can be invoked directly using the class name.
46
What problem would occur if main were not static?
JVM would need to create an object first, but it would not know which constructor or parameters to use.
47
Who calls the main method?
The JVM calls main as the entry point of the program.
48
Does JVM create an object to call main?
No. JVM calls main directly because it is static.
49
Is static main just a convention or a requirement?
It is a requirement defined by the JVM specification for program entry point.
50
Can Java run a program without a static main method?
Not as a standard application entry point; JVM requires static main to start execution.
51
What is the exact signature JVM looks for?
public static void main(String[] args)
52
Why must main be public?
So JVM can access it from outside the class.
53
Why must main be void?
Because JVM does not expect any return value.
54
Why must main accept String[] args?
To receive command-line arguments passed at runtime.
55
Can we overload main method?
Yes, but JVM only calls the standard signature version.
56
Can main be static but not public?
It compiles, but JVM cannot access it, so program will not run.
57
Can main be static final?
Yes. JVM does not restrict extra modifiers as long as required signature exists.
58
What would happen if main were instance-based?
JVM would fail to start because it cannot invoke instance methods without an object.
59
Why is static ideal for entry point design?
It allows execution to begin without requiring prior object initialization.
60
When do we use a static block in Java?
A static block is used to perform complex initialization of static variables when a class is loaded.
61
What is a static block in Java?
A static block is a block of code marked with static that runs once when the class is first loaded into memory.
62
When does a static block execute?
It executes before the main method and before any objects of the class are created.
63
Why use a static block instead of initializing variables inline?
Because static blocks allow multi-step logic, exception handling, and complex setup.
64
Example of static block syntax?
static { /* initialization code */ }
65
How many times does a static block run?
Only once per class loader.
66
Can a class have multiple static blocks?
Yes. They execute in the order they appear in the class.
67
What can static blocks access directly?
Only static variables and static methods.
68
Can static blocks access instance variables?
No, because they run before any objects exist.
69
What is the execution order: static block or constructor?
Static block runs first, then constructor runs when an object is created.
70
When is static block preferred over static method?
When initialization must occur automatically at class loading time.
71
Can static block throw checked exceptions?
No. They must handle exceptions internally.
72
What triggers static block execution?
Class loading by JVM.
73
Is static block required in every class?
No. It is only used when special static initialization is needed.
74
Real-world example use of static block?
Loading configuration values or initializing static database connections once.
75
Is it possible to run a Java program without a main() method?
No. For standard Java applications, JVM requires a main() method as the entry point.
76
Why does Java require a main() method to run a program?
Because JVM looks specifically for main(String[] args) to know where program execution should begin.
77
Did older Java versions allow execution without main()?
Earlier versions allowed tricks using static blocks in some environments, but modern Java requires a main() method.
78
Can static blocks replace main() as entry point?
No. Static blocks run during class loading, but JVM still requires main() to start execution.
79
What is the official entry point signature for Java programs?
public static void main(String[] args)
80
Can a class run without main if used differently?
Yes. Classes can run without main when used in environments like applets, servlets, frameworks, or tests where another system provides the entry point.
81
Who actually calls the main method?
The JVM runtime.
82
What happens if main() is missing?
The program compiles but fails at runtime with an error stating main method not found.
83
Is main() required for libraries?
No. Libraries do not need a main method because they are used by other programs.
84
Can you run Java code without main using tools like JShell?
Yes. Interactive tools can execute code snippets without main because they provide their own execution environment.
85
Why is main static?
So JVM can call it without creating an object instance.
86
What happens if main is not declared static in Java?
The program compiles, but JVM cannot find a valid entry point and throws an error at runtime.
87
Why does JVM require main to be static?
Because JVM calls main without creating an object, and only static methods can be invoked without instances.
88
What error occurs if main is not static?
The JVM reports an error like "Main method not found" because it cannot locate the required signature.
89
Will code compile if main is non-static?
Yes. The compiler allows it, but execution fails.
90
What exact signature does JVM look for?
public static void main(String[] args)
91
Can JVM call a non-static main method?
No. JVM does not create objects automatically to call instance methods.
92
Why doesn’t JVM instantiate the class automatically?
Because it would not know which constructor to call or what arguments to pass.
93
Does method name alone define entry point?
No. The method must match exact signature including static modifier.
94
What happens internally when java ClassName runs?
JVM loads the class and searches for a public static main method with correct signature.
95
Can main be static but not public?
It compiles, but JVM cannot access it, so execution fails.
96
Is static modifier optional for main?
No. It is mandatory for program execution.
97
What is the role of String[] args in main?
It receives command-line arguments passed to the program.
98
Why is JVM strict about main signature?
To ensure a consistent and predictable entry point for all Java applications.
99
What is the difference between static methods and instance methods in Java?
Static methods belong to the class and do not require objects, while instance methods belong to objects and require an instance to be called.
100
When should you use a static method?
When behavior does not depend on instance variables and should be shared across all objects.
101
When should you use an instance method?
When behavior depends on object state or instance variables.
102
How do you call a static method?
ClassName.methodName().
103
How do you call an instance method?
objectReference.methodName().
104
Can a static method access instance variables directly?
No, because instance variables belong to objects and static methods have no object context.
105
Can an instance method access static variables?
Yes. Instance methods can access both static and instance members.
106
Why can static methods not use this?
Because this refers to a specific object, and static methods are not tied to any instance.
107
Can static methods call instance methods?
Only if they create or receive an object reference first.
108
Can instance methods call static methods?
Yes, directly without any object.
109
Memory difference between static and instance methods?
Static methods exist once per class, while instance methods conceptually belong to each object instance.
110
Which method type supports polymorphism?
Instance methods support runtime polymorphism; static methods do not.
111
What happens if a subclass defines a static method with same signature?
It hides the parent method rather than overriding it.
112
Real-world example of static method?
Math.max() because it does not depend on object state.
113
Real-world example of instance method?
String.length() because it depends on a specific string object.