my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessing super class instance variable 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 "Accessing super class instance variable" Watch "Accessing super class instance variable" New topic
Author

Accessing super class instance variable

Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 278

[Added code tags - see UseCodeTags for details]

super.x should have printed 10;

and what if i mark the variable x as static.


Oracle Java Programmer , Oracle SQL Expert , Oracle Java Web Component Developer
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3603
    
  15
Mahtab Alam wrote:super.x should have printed 10;

Why ? You set it to 12 on the previous line.


Joanne
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 278

A bit explanation will help , still didn`t get it

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Mahtab Alam wrote:A bit explanation will help , still didn`t get it

Thanks


In the previous line you set x to 12... Notice that you did *NOT* declare a local variable, nor is there an instance variable in the subclass -- so the compiler is smart enough to know that you mean to set the instance variable that was declared in the super class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

How many x variables do you think you have there?
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 278

I was horribly wrong i thought x is instance variable in subclass since it extends .
subclass is just using the super class instance variable.
 
GeeCON Prague 2014
 
subject: Accessing super class instance variable