aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Initializers. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Initializers." Watch "Static Initializers." New topic
Author

Static Initializers.

Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
Hi All,
Here is one more teaser if you have time to answer
What will be the result of attempting to compile and run the following class?
public class InitTest
{
static String s1 = sM1("a");
{
s1 = sM1("b");
}
static
{
s1 = sM1("c");
}
public static void main(String args[ ] )
{
InitTest it = new InitTest( );
}
private static String sM1(String s)
{
System.out.println(s) ; return s;
}
}
a)The program will fail to compile
b)The program will compile without error and prints a,c,b in that order when run.
c)The program will compile without error and prints a,b,c in that order when run.
d)The program will compile without error and prints c,a,b in that order when run.
e)The program will compile without error and prints b,c,a in that order when run.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
is it b ?
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
i also think it is b
Harry Chawla
Ranch Hand

Joined: Jun 03, 2000
Posts: 97
What I understand is that answer should be c), printing a, b, c respectively.
[This message has been edited by Harry Chawla (edited August 02, 2000).]
Amandeep Waraich
Ranch Hand

Joined: Jul 14, 2000
Posts: 56
I also think that the answer should be b.
Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
Hi All,
The answer is b,it prints a,c,b in that order.Here are some of the points:
First, static statements/blocks are called IN THE ORDER they are defined.
Next, instance initializer statements/blocks are called IN THE ORDER they are defined.
Finally, the constructor is called. So, it prints a, c an b in that order.
Surya
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Surya B:
Hi All,
The answer is b,it prints a,c,b in that order.Here are some of the points:
1. First, static statements/blocks are called IN THE ORDER they are defined.
2. Next, instance initializer statements/blocks are called IN THE ORDER they are defined.
3. Finally, the constructor is called. So, it prints a, c an b in that order.
Surya

I want to add one more point here. After point 1, if the constructor has this() or super() that will be executed.
[ ofcourse there is none in Surya's example. But i just want to mention it]

This is the order
1. Static Initializer.
2. Constructor Header
3. Instance Initializer.
4. Constructor Code.

Folks, try out some code and correct me if I am wrong.

- Srini
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
Hi Vasan,
You forgot about class and instance field initialization outside
initializer blocks.
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
Here is the initialization sequence if you consider the
inheritance hierarchy.
1. When class is loaded
All static fields initialization and static initializers are
invoked from top down from Object down through inheritance
hierarchy to the class being loaded.

2. When an object is being created
Initialization is again done top-down from Object to the
class of the object being created.

Instance initialization is done in the following order:
a. Any field initializers and instance initializers
b. constructor
Here is a sample code demonstrating initialization sequence.

The output is:
1.accessing static variable
2.Parent static field
3.Parent static initializer block
4.Child static field
5.Child static initializer block
6.constructing object
7.Parent instance field
8.Parent instance initializer block
9.Parent Constructor
10.Child instance field
11.Child instance initializer block
12.Child Constructor
Please let me know if I missed out something
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Initializers.