• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static method initialization

 
Kevin Lam
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Try it and see...
 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..Alpana..

Check this thread..

Explicit Constructor

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic