jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes value of instance variable in super vs sub class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "value of instance variable in super vs sub class" Watch "value of instance variable in super vs sub class" New topic
Author

value of instance variable in super vs sub class

R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Hi all. I saw this question on Java Inquisition (http://enigma.vm.bytemark.co.uk/inquisition/index.php/Main_Page):



Question: What will the above code print? I was thinking that st.x would be 3 since x is a member variable. But its not. Can someone please help me understand why? TIA.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Access to instance variables is not polymorphic like non-static methods. It works just like static methods (this might help)....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
only instance methods are overridden , variables are never overridden. the st is superb type variable so it gets the value of x in superb class. it does not check what instance is in st variable.


SCJP 6.0 (86%) SCWCD 5 (94%)
R Srini
Ranch Hand

Joined: Feb 19, 2010
Posts: 215
Thank you for the clarification, guys!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: value of instance variable in super vs sub class
 
Similar Threads
One more time :) G. C. Q and A
concept of static and instance variable
Garbage Collection Q & A
a public thingo at the start
static and instance variables