*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt 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 "Doubt" Watch "Doubt" New topic
Author

Doubt

sravanthi pulukuri
Ranch Hand

Joined: Mar 15, 2007
Posts: 125
public class InitTest
{
public InitTest()
{
s1 = sM1("1");
}
static String s1 = sM1("a");
String s3 = sM1("2");
{
s1 = sM1("3");
}
static
{
s1 = sM1("b");
}
static String s2 = sM1("c");
String s4 = sM1("4");
public static void main(String args[])
{
InitTest it = new InitTest();
}
private static String sM1(String s)
{
System.out.println(s); return s;
}
}



The output was given as a b c 2 3 4 1
can any one explain me how is that possible??
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
1.Static blocks and static members will be instatiated first in the order they are in the code.
2.non static instance variables and blocks will be executed in the order
3.Constructor is then executed as called in main.


First static member initialization with the stmt
static String s1 = sM1("a");
and first o/p is a
Next static block in the order is
{
s1 = sM1("b");
}
thus b is printed.
Next in the order comes static member initialization and c gets printed

Now comes, nonstatic members and blocks initialization from top to bottom
String s3 = sM1("2"); will be executed and thus prints 2
next comes non static block in the order
{
s1 = sM1("3");
}
prints 3
next instance variable initialization
String s4 = sM1("4"); will be executed and thus prints 4

finally thus constructor will be executed and prints 1

thus the final o/p is
a b c 2 3 4 1
Ananth Majumdar
Greenhorn

Joined: Aug 17, 2006
Posts: 20
static blocks and static variables are executed at compile time only.

Flow:
1. Static variables are initialized in the order they are declared first 'a' then the static block for b is executed then 'c'.
2. Then the new InitTest(); is called before the constructor is executed all the instance variables are initialized in the order they are declared. first '2' then '3' then '4' then the constructor is invoked and finally it calls the method with '1' and it prints it.

so it prints a b c 2 3 4 1
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,


You code execution and variable assignment takes place in following order:



1- When class is loaded, all the static initializer executes in order they appear in the class.
2- Then all instance initializer executes in the order they appear.
3- Then costructor completes.

That is the way you got the output.

Hope this makes you clear you doubt!
Thanks and Regards,
cmbhatt


cmbhatt
sravanthi pulukuri
Ranch Hand

Joined: Mar 15, 2007
Posts: 125
Regarding static varaibles and stactic blocks it was fine
but what i thought was instance variables are intialised after constructors run.can you empasize more on this issue
thanks for the fast reply
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt
 
Similar Threads
Help with class initialization
Static Initializers.
Static and Instance Blocks..
Initializing Sequence
initialization sequence - what is done first?