File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static method initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static method initialization" Watch "static method initialization" New topic
Author

static method initialization

Kevin Lam
Ranch Hand

Joined: Oct 27, 2005
Posts: 68
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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Kevin Lam
Ranch Hand

Joined: Oct 27, 2005
Posts: 68
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

Joined: Aug 31, 2004
Posts: 11343

No. Try it and see...
Alpana Singh
Ranch Hand

Joined: Sep 27, 2005
Posts: 36
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

Joined: Jul 04, 2004
Posts: 978
Hi..Alpana..

Check this thread..

Explicit Constructor

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
 
subject: static method initialization
 
Similar Threads
Why it is illegal to use abstract and static modifiers together in method declaration ?
polymorphism
Master exam question on inner class
return types scjp1.4
Using final as program-wide variables