Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Instance variable 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 "Instance variable initialization" Watch "Instance variable initialization" New topic
Author

Instance variable initialization

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
When the instance variables are initialized?During compile time or during runtime?
Thank
Veena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Instance variables are initialized at run time after the constructor starts to run and after the implicit or explicit call to the super class constructor but before the body of the constructor is processed. Static variables are initialized when the class is loaded.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Then in the following code why s1 is null?

class G {
String s1 = "G.s1";
void printS1(){System.out.print("G.printS1," + s1);}
G() { printS1();}
}
class H extends G {
String s1 = "H.s1";
void printS1(){System.out.print("H.printS1," + s1);}
public static void main(String[] args) {
H h = new H();
}
}

This was one of the question in ur mock exam.O/p is H.printS1,null.How s1 is null?Shouldn't it be H.s1?
Veena
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Veena Point:
When the instance variables are initialized?During compile time or during runtime?

Variables are never initialized at compile time. The compiler only generates byte codes. It doesn't create variables. That's a job for the JVM at runtime.
A better question would be, "Are instance variables initialized at class load time or at class instantiation time?"
In that case, instance variables are initialized at class instantiation time - you need an instance to have an instance member. Static members, however, are initialized at class load time.
I hope that helps,
Corey


SCJP Tipline, etc.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The reason you're getting the null value is because the printS1 method is overridden. First, the parent's (G) constructor is invoked, which then invokes the method printS1 from the class H. This method prints the value of s1, which shadows the s1 in G. As the members of class H have not yet been initialized (we initialize parent classes first), s1, in H is still null.
I hope that helps,
Corey
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
if H1 is null, then what does line 2 do ?
class H extends G { //line 1
String s1 = "H.s1"; //line 2
void printS1(){System.out.print("H.printS1," + s1);}
public static void main(String[] args) {
H h = new H();
}
}


SCJP 1.4
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Thanks Corey and Dan.
Veena
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Maria,
Your line 2 initializes H.s1 after the super class constructor runs to completion. However, the null value is printed from within the super class constructor. Therefore, H.s1 still contains the default value of null.
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
oh, got it! thanks Dan
 
 
subject: Instance variable initialization