• 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

accessing SuperClass static variable in subclass

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


just wanted to confirm my understanding on how we can access superclass static variable in a subclass.
eg
Class SuperClass
{
static int x =5;
}
Class SubClass extends SuperClass{
//access x from here
}


1. One way is to use the Superclass name eg SuperClass.x

2. access by reference to a superclass instance
eg SuperClass A = new SuperClass();
A.x

3. access by polymorphic instantiation ie
SuperClass A = new SubClass();
A.x

Please correct me if I am wrong and also is there any other way to access x from .....//access here in the above code....

Going crazy with static accesses....Phew...!!

mary
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also you can say 'SubClass.x' because x is static, or just say 'x' because SubClass inherited that variable.
 
Mary John
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

and please help me with this static acess...

thats one thing I was confused with. I tried to compile this code that accesses the superclass static variable y in the subclass. But code does not compile it gives following error....
super(y) cannot find symbol, variable y
SubClassStatic.y cannot find symbol:variable y

Class CheckStatic{
static int y =2;
CheckStatic(int x) {x=x*2;};

}
public class SubClassStatic
{
SubClassStatic(){super(y);}


public static void main(String[] args){
System.out.println(SubClassStatic.y);
}
 
Mary John
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pplease ignore the above post, i forgot to extend in the subclass
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic