aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initialization" Watch "Initialization" New topic
Author

Initialization

Shah Chunky
Ranch Hand

Joined: Dec 27, 2000
Posts: 116
Hello all...
Can someone explain me why in the below porgram, the Value of m is 0
public class Numeric
{
public static void main(String[] args)
{
Numeric a = new Numeric(x);
System.out.println();
}
static int i = 5;
static int x;
int j = 7;
int k;

Numeric(int m)
{
System.out.println("I "+i);
System.out.println("J "+j);
System.out.println("K "+k);
System.out.println("X "+x);
System.out.println("M "+m);
}
{
j=70;
x=20;
}
static
{
i=50;
}
}

Thanks

Shah Chunky - Sun Certified Java2 Programmer.
srinivasrao marri
Ranch Hand

Joined: Feb 13, 2001
Posts: 61
hi,
for this u need to know the initialization rules of instance variables and local variables.
1. an instance variable is one which is declared within a class but not in an method or block. for these variables the initialization is done by default.
2. a local variable is one which is specific to a method or a block where u need to initialize explicitly.
if u see u'r program in this perspective u can get clarified
thank you
srinivas
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
static initializations, is executed only once, the first time you make an object of that class or the first time you access a static member of that class (even if you never make an object of
that class).
I Think The Order of Initialization is :
1. The first time a static method (which is main method)or of class "Numeric" is accessed, the Java interpreter must locate Numeric.class, which it does by searching through the classpath.
2. As Numeric.class is loaded (creating a Class object), all of its static initializers are run. Thus, STATIC INITIALIZATION TAKES PLACE ONLY ONCE, as the Class object is loaded for the first time.
in your code :
static int i = 5;
static { i=50; } //you try to change i within static block initializer, this is OK, cooz' it's part of the first loaded class.
static int x; //this will give zero by default
{ j = 70; x = 20 } //you try to change x within instance block initializer, this is NOT part of the first loaded class, couz' it's belong to the instance of the class.
thus you still have x = 0.
I bet when you create another Numeric object, you'll get m = 20.
stevie
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
hi,
it's true that static varaible's and static block's are executed during the class load time.But instance variable's are initialized when the constructor of the class is excuted.Now when u create an object of the Numeric class which in your case is a then your constructor get's called at that moment it passes the value of (x=0)to the variable varaible m and then it calls the constructor of the superclass after executing the constructor of the superclass the control come's back to the previous constructor and then it initializes the instance variable of it's object.when u say System.out.println(m) it show's the value of of variaible m which in our case is 0.
Dave Terrian
Greenhorn

Joined: Jun 25, 2000
Posts: 15
Wow, I was a little skeptical that it calls the super's constructor before initializing instance variables but you're right. By changing the code to this:

We see that x remains 0 until after the super is called.
Two questions:
Is this something you knew by experimentation or would it be found in the JLS or in a certification book? I've never seen the construct of an instance initialization block like that in my studies.
Is this really something you find in the exam?!
- Dave
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
It is all in the JLS in gory detail.
Yes, you absolutely need to know that the super class constructor is called before the subclass constructor is executed as well as the order of initialization.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initialization