aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About instance in. and static initializer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About instance in. and static initializer" Watch "About instance in. and static initializer" New topic
Author

About instance in. and static initializer

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
public class MyClass
{
public static void main(String[] args)
MyClass obj = new MyClass(l);
static int i=5;
static int l;
int j=7;
int k;
public MyClass(int m )
{
S.o.p(i + ", " + j + ", " + k + " ," + l + ", " + m );
}
{ j=70; l=20; }
static { i=50;}
}
Ans : 50,70,0,20,0
Above is an example from Khalid Mughal.
I got the answer as per the explanation given.
But i having some doubts
---------------------------------------------------------------
Below is what i have understood :
1. When the class is first initialized the static members are initialized
and static initializer blocks if any are executed.
2. The instance members at this same time to their default values.
3. Now when the instance of the class is created , the instance
variables are initialized and instance initializers are executed.
--------------------------------------------------------------
Below are my doubts :
Does this mean that static initializer blocks are executed before
the instance initializer blocks ?
Secondly is the execution of instance initializer blocks and variables
done just before execution of the constructor of the class ?
---------------------------------------------------------------
Can someone suggest me some good example which has a combination
of both instance initializer and static initializer ?
Thanks in advance !!
Sharada Kumaran
Greenhorn

Joined: May 23, 2001
Posts: 29
Yes, your understanding is correct.
The sequence of initialization is -
1. static initializers
2. instance initializers
3. constructors
As static code belong to the class, and not its instances, static initialization is done at the begining, when the class is loaded the first time, and only once.
When an instance is created, the instance intialization is done, followed by the constructors.
If you run the folowing code, you may understand better.
This also illustrates the sequence in the case of inheritance.
class A
{
A()
{
System.out.println("In constructor SUPER___3");
}
static int a = geta();
static int geta()
{
System.out.println("in static method SUPER___1");
return 1;
}
{
System.out.println("in instance method SUPER___2");
}
}
class B extends A
{
B()
{
System.out.println("In constructor SUB___6");
}
static int a = geta();
static int geta()
{
System.out.println("in static method SUB___1/4");
return 1;
}
{
System.out.println("in instance method SUB___5");
}

public static void main(String args[])
{
new B();

}
};


Sharon
 
wood burning stoves
 
subject: About instance in. and static initializer