-1

I've read Statics in Java are not inherited. I've a small program below which compiles and produces 2 2 as output when run. From the program it looks like k (a static variable) is being inherited !! What am I doing wrong?

class Super
{
    int i =1;
    static int k = 2;
    public static void print()
    {
        System.out.println(k);
    }
}
class Sub extends Super
{
    public void show()
    {
        // I was expecting compile error here. But it works !!
        System.out.println(" k : " + k);
    }
    public static void main(String []args)
    {
        Sub m =new Sub();
        m.show();
        print();
    }
} 
gameover
  • 11,813
  • 16
  • 59
  • 70
  • Never heard that, your assumption is probably wrong. – Erich Kitzmueller Jan 29 '10 at 09:42
  • I read it here: http://stackoverflow.com/questions/1740528/inheritance-vs-static-in-java – gameover Jan 29 '10 at 09:43
  • 1
    If you read the comments on that, the poster is taking 'inherited' to mean 'inherited with the ability to override with polymorphic runtime dispatch', which is somewhat unconventional and liable to confuse. – Pete Kirkham Jan 29 '10 at 09:49
  • @Pete: I see, so I conclude that static *variables* of parent class are inherited by the child ? – gameover Jan 29 '10 at 09:51
  • 1
    @gameover: static variable are not *inherited*, they are **available**. – David Rodríguez - dribeas Jan 29 '10 at 10:10
  • @David: With respect to variables( not methods) how is being available and being inherited different ? – gameover Jan 29 '10 at 10:14
  • An public static member is available to all code in your application, but is not inherited anywhere. The same way that code in other classes can access that static member, code within a deriving class can access it. The only difference is that the name lookup rules dictate that then the compiler sees `k` it will try to match against a local variable, a member or static in this class, a member or static in any extended classes... The difference is probably more subtle than what most programmers really care about. There is only one `k`, the one in the base class. – David Rodríguez - dribeas Jan 29 '10 at 10:45

5 Answers5

3

The scope in which names are looked up in includes the super class.

The name print is not found in Sub so is resolved in the Super.

When the compiler generates bytecode, the call will be made to Super.print, rather than a call on a method in Sub.

Similarly the k is visible in the sub-class without qualifying it.


There is no polymorphism here, only inheritance of the contents of a name space. Static methods and all fields do not have polymorphic dispatch in Java, so can only be hidden by sub-classes, not overridden. The post you link to in your comments is using 'inheritance' in a somewhat unconventional way, mixing it up with polymorphism. You can have polymorphism without inheritance and inheritance without polymorphism.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • Are "being visible" and "inherited" not the same ? – gameover Jan 29 '10 at 09:54
  • 1
    If class A has a public method "doSomething()", then class B can access A.doSomething(). This means that A.doSomething() is "available" for B to invoke. However, do you really think that B has "inherited" A.doSomething() ? ;-) – divesh premdeep Jan 29 '10 at 12:31
  • 1
    If B extends A, yes, what you describe is inheritance. If B does not extend A, then what you describe is use. You can simulate inheritance by using an object you delegate to; early VB COM did exactly that - it had polymorphism, but not inheritance. – Pete Kirkham Jan 30 '10 at 10:40
0

Sub extends Super so it can see all the public/protected/package (static) members of Super.

I guess this is what you described as "Statics in Java are not inherited" change

static int k = 2;

to

private static int k = 2;

and your Sub program won't see 'k' anymore and won't compile;

also try to create a new 'static int k=3;' in Sub, and see what happens.

Pierre
  • 34,472
  • 31
  • 113
  • 192
0

It's pretty much the same as accessing Math.PI or any other global constant (which also have public and final modifiers).

In your case you have default (package) scope.

It's independed of inheritance only the scope restricts whether it is visible.

stacker
  • 68,052
  • 28
  • 140
  • 210
-1

I think you might be wrong about static variables not being inherited. I suppose some properties of it are not inherited. For instance a static var normally means that all instances of the class have access to the same place in memory.

When you inherit, the derived class does not refer to the same memory as the base class.

mlathe
  • 2,375
  • 1
  • 23
  • 42
  • 1
    The last sentence is false. If you only define the static variable in the base class, there will only be one instance of the variable in memory. All references to that static member (from either the base class or derived classes) will modify the single instance. – David Rodríguez - dribeas Jan 29 '10 at 10:17
-1

Static member can be static data member and static method which can be accessed without using the object. It is used by nested class