2

I have two top-level classes(BaseClass and B), BaseClass has main method in it and also has inner class(named Laptop). I know that static block is executed whenever class is loaded but for the following code:

           package kkk;

public class BaseClass 
{

static BaseClass e=new BaseClass();


public BaseClass()
{
System.out.println("In baseclass constructor");
}

{
System.out.println("in baseclass nonstatic block");
}

static
{
System.out.println("in baseclass static block");
}

protected static class Laptop
{
int x=8;

public Laptop()
{
    System.out.println("Inside laptop class");
}

void m1()
{
    System.out.println("Inside inner class method");
}
}



public void hem()
{
System.out.println("In base class hem");
}



public static void main(String args[])
{

e.hem();
System.out.println("In main method baseclass");
B h=new B();
h.bhel();
}
}

class B { void bhel() { BaseClass.Laptop obj=new BaseClass.Laptop(); System.out.println(obj.x); obj.m1(); } }

By running above code, i am getting output as:

in baseclass nonstatic block
In baseclass constructor
in baseclass static block
In base class hem
In main method baseclass
Inside laptop class
8
Inside inner class

e is static reference variable and memory must be allotted to it. So, static block is executed. But, why non-static block is executed before static block??

0

1 Answer 1

7

All static variable declarations and static initialization blocks are evaluated/executed in the order they appear in the source code when the class is initialized.

static BaseClass e=new BaseClass();

this creates an instance of BaseClass when the class is initialized and causes the instance initialization block to be called before the constructor is executed.

Since this line appears before the static initialization block, it is executed before that block, which means the instance initialization block is called before the static initialization block.

Sign up to request clarification or add additional context in comments.

5 Comments

shouldn't BaseClass be loaded before instantiation? causing the static block to be called before non-static block?
@PrasadKharkar The correct name of "non-static block" is instance initialization block. It is called before the constructor whenever you create an instance of the class. Since part of the initialization of BaseClass involves creating an instance of BaseClass, that instance is created before the class's static block is executed.
hmm now I think this makes sense. If we had simply written main method in BaseClass, then the static block would have executed, but as this is creating an instance from another class, it is calling instance initializer first, did I understand correctly?
Doesn't have anything to do with Main. If you look at the code again, BaseClass has some static members that need to be loaded into memory. As part of this initialization a new instance of BaseClass is created to be initialized to a static variable.
@PrasadKharkar I don't think so. The instance initializer is called before the static initializer only due to the static BaseClass e=new BaseClass(); line appearing before the static initializer block. Moving the main method to BaseClass wouldn't change that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.