• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Inheritance and static members

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have my exam in 10 days and would like an answer for this question: Do static members get inherited?
I compiled and run this code:
class Q1{
public static int numero = 10;
}
public class Q2 extends Q1{
public static void main(String argv[]){
Q2.numero++;
System.out.println("Q2.numero = " + Q2.numero + " Q1.numero = " + Q1.numero);
}
}
The output is:
Q2.numero = 11 Q1.numero = 11
So I can acces numero through Q2 with Q2.numero, but there is only one "numero" for both classes.
So static members don't get inherited. Am I right or not?
Please help
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is only one copy for static numero;
For example, you can access it by saying Q2.numero so it is visible within the Q2 class. However, if you reassign it a value by using the prefix(++Q2.numero) then it will show the results for both Q2.numero and Q1.numero in your System.out.println statement. This is because there is only one memory address allocated for the numero variable, so any reference to it that changes it, will change the value that is being stored, therefore, Q1.numero reference to it will display a value change as well. See the following code:
public class Q2 extends Q1
{
//static int a = numero;
public static void main(String argv[])
{
//static int a = numero;
//++a;
++Q2.numero;
System.out.println("Q2.numero= "+ Q2.numero + "
Q1.numero = "+Q1.numero);
//System.out.println("Q2.a= "+ Q2.a + "
//Q1.numero = "+Q1.numero);

}
}
If you wanted to change the value for it in only Q2, you would have to assign it to a different variable first(see //comments in above code - uncomment those and comment out lines with
++Q2.numero;
System.out.println("Q2.numero= "+ Q2.numero + "
Q1.numero = "+Q1.numero);
Try this out and see what happens. When you uncomment the line
//static int a = numero;
you are assigning the numero value to int a without using Q1.numero as a reference. This tells me that numero is inherited
and visible within Q2 but ultimately there is only one memory location for the static variable numero.
Also, you'll need to use the prefix operator(++a) instead of a++ to see different value results that help illustrate things.
 
Mafalda Alabort
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks David.
We agree that there is only one copy of the static variable, and that it is accessible from the subclass.
But the thing is:
Does the fact that it is accessible mean that it is inherited?
or
Does the fact that there is only one static vble for both class and subclass mean that it is not inherited?

 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't find the direct reference in the JLS that explicitly states that static members are inherited, however I found a quote that says:


Inner classes may inherit static members that are not compile-time constants even though they may not declare them.


So even though there is only one variable when it is static, it is considered to be inherited.
Also read this on static methods


8.4.8.5 Example: Invocation of Hidden Class Methods
A hidden class (static) method can be invoked by using a reference whose type is the class that actually contains the declaration of the method. In this respect, hiding of static methods is different from overriding of instance methods. The example:
class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
class Test {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());
}
}
produces the output:
Goodnight, Dick
because the invocation of greeting uses the type of s, namely Super, to figure out, at compile time, which class method to invoke, whereas the invocation of name uses the class of s, namely Sub, to figure out, at run time, which instance method to invoke.


So even though static methods are "considered" to be inherited by the JLS, they can not be overridden in the same manner as non-static methods because over-riding happens at runtime and static methods are resolved at compile time. This is what causes all the confusion about the word "inherited".
[This message has been edited by Cindy Glass (edited March 18, 2001).]
 
Mafalda Alabort
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so static members are inherited.
Thanks for your help
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic