File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance variable initialization

 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the instance variables are initialized?During compile time or during runtime?
Thank
Veena
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Corey and Dan.
Veena
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, got it! thanks Dan
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic