This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: value of instance variable in super vs sub class
 
Similar Threads
a public thingo at the start
concept of static and instance variable
Garbage Collection Q & A
static and instance variables
One more time :) G. C. Q and A