This article includes several questions about static, with answers provided at the end.
Static Methods and Class Loading
Static Methods and Class Loading
class Test {
static {
System.out.println("Static block");
}
public static void display() {
System.out.println("Display method");
}
}
public class Main {
public static void main(String[] args) {
Test.display();
}
}
What will be the output?
See the answer
Static block
Display method
Explanation:
When we call Test.display()
, the Test
class is loaded for the first time. This triggers the static block, which runs only once, printing "Static block"
. After that, the display
method is called, printing "Display method"
.
Static Methods vs. Instance Methods
class Example {
static void staticMethod() {
System.out.println("Static method");
}
void instanceMethod() {
System.out.println("Instance method");
}
}
public class Main {
public static void main(String[] args) {
Example obj = null;
obj.staticMethod();
obj.instanceMethod();
}
}
Will this code compile? If not, why?
See the answer
Static method
Exception in thread “main” java.lang.NullPointerException: Cannot invoke “tests.Example.instanceMethod()” because “obj” is null
Explanation:obj.staticMethod()
works even though obj
is null
because static methods are called directly on the class and don’t require an instance. However, obj.instanceMethod()
will throw a NullPointerException
because instance methods need a valid object reference.
Instance and Static Initializer Blocks
class Test {
{
System.out.println("Instance initializer");
}
static {
System.out.println("Static initializer");
}
public Test() {
System.out.println("Constructor");
}
}
public class Main {
public static void main(String[] args) {
new Test();
new Test();
}
}
What will be the output?
See the answer
Static initializer
Instance initializer
Constructor
Instance initializer
Constructor
Explanation:
The static block runs only once when the class is loaded. The instance initializer block and the constructor run every time a new object is created, so each time we create a Test
instance, they execute in order: instance initializer, then constructor.
Static Block Execution Order
class A {
static {
System.out.println("Static block in A");
}
}
class B extends A {
static {
System.out.println("Static block in B");
}
}
public class Main {
public static void main(String[] args) {
B obj = new B();
}
}
What will be the output?
See the answer
Static block in A
Static block in B
Explanation:
When a class is loaded, all static blocks in its inheritance hierarchy execute from top to bottom. First, the static block in class A
runs, followed by the static block in class B
. This happens before the B
object is created.
Static Variables and Block Execution
class Example {
static int count;
static {
count = 10;
System.out.println("Static block: " + count);
}
{
count++;
System.out.println("Instance block: " + count);
}
public Example() {
count += 2;
System.out.println("Constructor: " + count);
}
}
public class Main {
public static void main(String[] args) {
new Example();
new Example();
System.out.println("Final count: " + Example.count);
}
}
What will be the output?
See the answer
Static block: 10
Instance block: 11
Constructor: 13
Instance block: 14
Constructor: 16
Final count: 16
Explanation:
The static block initializes count
to 10 and prints "Static block: 10"
.Each time an instance of Example
is created, the instance initializer increments count
by 1, and the constructor further increments it by 2.The sequence for each Example
object is: instance block, constructor.
Leave a Reply