File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initializers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initializers" Watch "Initializers" New topic
Author

Initializers

Arunv Kannapiran
Greenhorn

Joined: Sep 07, 2004
Posts: 1
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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
harsha rayudu
Greenhorn

Joined: Sep 08, 2004
Posts: 2
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

Joined: Sep 08, 2004
Posts: 2
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

Joined: Aug 31, 2004
Posts: 11343

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

    Joined: Sep 02, 2004
    Posts: 83
    This is an interesting article discussing static initialization:

    http://www.developer.com/java/other/article.php/2238491
    Oneal Shaha
    Ranch Hand

    Joined: Aug 11, 2004
    Posts: 98
    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


    -Oneal
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Initializers
     
    Similar Threads
    static initializer question from khalid mughal
    About instance in. and static initializer
    a question..
    Static Int and Methods
    Initializer Blocks(Static and Instance)