0

I come to you with a simple OOP question. Let's say there are three classes - A, B and C. B and C both inherit a static variable from A. Let's also say that A defines the value of this variable as being 0. Let's say that both B and C modify the value of this static variable. My question is this - since static variables remain constant across members of the same class, does this mean that setting the value of a variable of a superclass from a subclass will have no effect on the value of the super class?

In other words, if we change the value of B's variable to 1, that value will only be 1 for objects of class B and not C or A?

  • Why not test it and find out? What do you think should happen? What makes you think so? – Pshemo Apr 22 '17 at 15:41
  • Anyway in title you are saying about overriding, but in your question you said "both B and C modify the value of this static variable" which is closer to reassigning value. Maybe try to clarify that part by posting proper code example. – Pshemo Apr 22 '17 at 15:43
  • 3
    Possible duplicate of [Are static variables inherited](http://stackoverflow.com/questions/37226269/are-static-variables-inherited) – djm.im Apr 22 '17 at 16:13
  • You cannot override a variable, much less a static variable. You can only override an instance method. – Lew Bloch Apr 22 '17 at 16:28

3 Answers3

2

A static field only belongs to one class and is not inherited by its sub-classes

mojth
  • 44
  • 3
  • 2
    "and is not inherited by its sub-classes" is vague since subclass can inherit *access* to this static field. Lets say we have `class Foo{public static int x = 0;}` and `class Bar extends Foo{}`. This means we can still use `Bar.x` to access `x` defined in `Foo`. – Pshemo Apr 22 '17 at 16:00
  • You are right, the sub-classes have access, but from a OO point of view, there is only one field and it belongs to the super-class – mojth Apr 22 '17 at 16:28
  • Static fields _are_ inherited by subtypes​, if accessible and not private. Per the JLS, "A class inherits from its direct superclass and direct superinterfaces all the non-private fields of the superclass and superinterfaces that are both accessible to code in the class and not hidden by a declaration in the class." This answer is flat-out wrong. – Lew Bloch Apr 22 '17 at 16:34
  • This is a rather academic discussion, but what you (and apparently the the Documentation) are describing I would not call inharetace from a OO point of view. – mojth Apr 22 '17 at 16:50
0

Here you can find simple java implementation for your problem.
The results can interpret solution.

 class A {
   static int variable = 0;
}

class B extends A{

}
 class C extends A{

}

public class Runclass {

    public static void main(String arg[]){
        A a = new A();
        System.out.println("A "+a.variable);

        B b = new B();
        System.out.println("B "+b.variable);

        C c = new C();
        System.out.println("C "+c.variable);

        b.variable=1;
        System.out.println("After modifying B "+b.variable);
        System.out.println(" A "+a.variable);

    }
}

Output:

A 0
B 0
C 0
After modifying B 1
A 1
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
Dasarathan
  • 63
  • 5
0

static doesn't mean constant. final means that you can't change the value.
You can reach the variable in the superclass A and change it, using this code for example in B: A.someVariable=1;
After this, you will see the value 1 from C, B and A too, if you use the superclass A variable (A.someVariable).
I give you a code example:

package teszt;

class A {
    static int statValami=0;
}


package teszt;

class B extends A{
    void writeOut(){
        A.statValami=1;
        System.out.println(statValami);
    }
}


package teszt;

class C extends A {
    void writeOut(){
        A.statValami=100;
        System.out.println(statValami);
    }
}


package teszt;

class Over  {
    public static void main(String args[]){
        new B().writeOut();
        new C().writeOut();
        System.out.println(A.statValami);
    }
}



OUTPUT is:
1
100
100

Colossus
  • 41
  • 1
  • 10