• 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

Inhertiting static member variable !

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does the static memeber variable behaves towards inheritance? For e.g

class X {
static int a;
}

class Y extends X{ }

Class Z extends Y{

public static void main(String[] S){
X x = new X();
S.o.p(X.a);
x.a++;
Y y = new Y();
S.o.p(y.a);
Y.a++;
S.o.p(Z.a);
}
}

Please clarify any other behaviours of static variable towards inheritance, with respect to memory allocation, stack, heap and object reference.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prathamesh,
There is only one A variable no matter how many instances or subclasses you have. While all of those references do refer to the same A variable, the code is harder to read. It is better to always refer to the static variable through the class hosting it - in this case X. Otherwise, someone reading the code has to figure out that there are three different ways of referring to the same variable in the method.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prathamesh Gaddam:
How does the static memeber variable behaves towards inheritance?


Until a subclass hides it, a static field or method is available through the class itself, its subclasses and instances of all those classes. However, it is also available through null references! All that matters for those is the class the reference is declared as:

Although x is null, it is declared as being of class X, so x.A actually executes X.A.

That code also shows something that has been allowed in previous versions of Java: although the array index is invalid, Java 1.3 (I believe) allowed it and would print 5 - since only the declared type (X) was used.
This is no longer possible though.


Now, if a subclass of X called Y would also declare a static field called A, then the declared type is what matters:
 
Prathamesh Gaddam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amazing! It's what I call it as real fundu! Dhanyavaad (Thank you in Hindi).

Please confirm as what I have understood.

In the hierarchy of class and its sub-class a single copy of static variable is maintained on heap which can be referred by null or class-type reference.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While it is legal Java to invoke a static method using an object reference (which may be null), it is very bad practice to do so. You should always invoke it using the class literal.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic