• 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

Static Initializers.

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it b ?
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i also think it is b
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also think that the answer should be b.
 
Surya B
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vasan,
You forgot about class and instance field initialization outside
initializer blocks.
 
rajsim
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic