• 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 method initialization

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

class Test{
public static Foo f = new Foo();
public static Bar b = new Bar();

public static void main()
{
....
}
}

Would f and b get initialized before Test is initialized?

Kev
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Lam:
... Would f and b get initialized before Test is initialized?


When a class is loaded, static variables are initialized and static blocks are executed. If an object is created, then instance variables are initialized and non-static initializer blocks are executed, followed by the constructor body.

For extended classes, you might find the output of the following code interesting...

[ November 02, 2005: Message edited by: marc weber ]
 
Kevin Lam
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your example prints out

SubClass initialiser block
SubClass static block
SuperClass initialiser block
SuperClass static block
SuperClass constructor
SubClass constructor

?

Kev
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Try it and see...
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a class is loaded, static variables are initialized and static blocks are executed. If an object is created, then instance variables are initialized and non-static initializer blocks are executed, followed by the constructor body.

Why does this code give an error

public class T1{
int y;
T1(){
this(y);
}

T1(int k){
System.out.println(k);
}
public void amethod(){
y=2;
}

public static void main(String args[])
{
T1 tt=new T1();
}
}


Since the instance variable are initialised before constructor runs,it should have value of y=0.
 
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..Alpana..

Check this thread..

Explicit Constructor

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alpana Singh:
... Since the instance variable are initialised before constructor runs,it should have value of y=0.


Constructor execution is a little more complicated. The first line in any constructor must be some call to this or super (that is, an overloaded constructor for the same class, or a superclass constructor). If that line isn't there explicitly, then it's implicit as a call to super(). Instance variables are initialized after this line of the constructor executes, but before the remainder of the constructor body executes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic