• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic