Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializers

 
Arunv Kannapiran
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below code was taken from A Programmer�s Guide to Java Certification, 2nd edition:

public class MyClass {
public static void main(String[] args) {
MyClass obj = new MyClass(l);
}

static int i = 5;
static int l;
int j = 7;
int k;

public MyClass(int m) {
System.out.println(i + �, � + j + �, � + k + �, � + l + �, � + m);
}

{ j = 70; l = 20} // Instance initializer block
static {i = 50; } // Static initializer block
}

the output of the above code is: 50, 70, 0, 20, 0

I assumed that the constructor is run (which would mean that the initializer block is executed) before the main method is executed which would make the value of m = 20 but this was not the case. My question is if the constructor is only called if an instance of the class is created with �new�. ie if the object �obj� is not created would the constructor still be called ?

I also thought the initializer block is executed before execution of the main method is started, but this does not appear to be the case� ?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static variables are initialized and static block is executed at class load time, which occurs either when the first object of that class is created or when a static member of the class is accessed (even if no object is created).

The main method is static.

Now, if an object is being created, then instance variables are initialized and non-static initializer block is executed. After this, the body of the constructor executes.

Inside the main method of this code, a constructor is called to create an object.

Indeed, your output demonstrates that by the time the constructor body executes, the initializer block has already set j =70 and l = 20.

But consider this: The argument to the constructor is a primitive, so the value itself (rather than a reference) is passed. At class load time, l is initialized to 0, and this the value passed to the constructor. By the time the non-static initializer block sets l = 20, the variable m (which is local to the method) has already been set to the passed value of 0.
[ September 10, 2004: Message edited by: marc weber ]
 
harsha rayudu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When exactly is the instance initializer block
{ j = 70; l = 20}
executed, before the constructor, after the constructor or as a first statement of the constructor after the call to super classes constructor?
 
harsha rayudu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My testing shows that, it is executed in the constructor right after any implicit or explicit calls to the constructor of the super class.

public class initTest
{
public static void main(String[] args)
{
initTest obj = new initTest(l);
}
static int i = 5;
static int l;
int j = 7;
int k;

public initTest(int m) {
System.out.println(i + ", " + j + ", " + k + ", " + l + ", " + m);
}

{ j = 70; l = 20; System.out.println("In the instance initializer block");} // Instance initializer block
static {i = 50; System.out.println("In the static initializer block");} // Static initializer block
}

Gives me the output of,

In the static initializer block
In the instance initializer block
50, 70, 0, 20, 0
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that non-static initializer block is executed after the superclass constructor is called, but before the rest of the current constructor body executes.

So when superclasses are involved: First (at class load), static variables are initialized and static block executes in both the parent and subclass. Next (if an object is being created), from the base class downwards, instance variables are initialized, non-static initializer block executes, and the constructor body executes.

That is...
  • Parent static.
  • Subclass static.
  • Parent initializer.
  • Parent constructor.
  • Subclass initializer.
  • Subclass constructor.


  • (Please correct me if I'm wrong.)
     
    Tom Tolman
    Ranch Hand
    Posts: 83
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is an interesting article discussing static initialization:

    http://www.developer.com/java/other/article.php/2238491
     
    Oneal Shaha
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by harsha rayudu:
    When exactly is the instance initializer block
    { j = 70; l = 20}
    executed, before the constructor, after the constructor or as a first statement of the constructor after the call to super classes constructor?



    I remeber while preparing for certification each time when we come to this question me and my friend had a gread dicussion and confusion

    At the end we decided that the non-static initializer blocks are called after the starting curly bracked of the constructor.
    Now Marc's approach is more logical.

    So his answer is perfect... It is called after the super() thing but before any other statements in constructor...

    It was a great dicussion. thanks all for conttrubution
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic