• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instance variable initialization

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the instance variables are initialized?During compile time or during runtime?
Thank
Veena
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Corey and Dan.
Veena
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, got it! thanks Dan
 
I once met a man from Nantucket. He had a tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic