-3

I have the following situation:

class A {
    static int a, aa;
    A(int x, int y) {
        a = x;
        aa = y;
    }
}

class B extends A {
    B() {
        super(2, 3);
    }
}

class C extends A {
    C() {
        super(3, 4);
    }
}

class Test {
    public static void main(String[] args) {
        B b = new B();
        C c = new C();
    }
}

Of course, now B.a is 3 and B.aa is 4.

How could I change B and C so that a and aa remain static, are still inherited from A, but can have a set of values for B and another set for C?

  • 4
    Possible duplicate of [What is the exact meaning of static fields in Java?](https://stackoverflow.com/questions/797964/what-is-the-exact-meaning-of-static-fields-in-java) – SamHoque Oct 26 '18 at 08:21
  • 8
    You can't call super(2,3) when you don't have any such constructor. Moreover static variables don't participate in object orientation. – Pushpesh Kumar Rajwanshi Oct 26 '18 at 08:21
  • A constructor is not a good place to initialize static variables – Maurice Perry Oct 26 '18 at 08:36
  • 1
    You probably want to add static variables `a` and `aa` to both `B` and `C`. – Robert Kock Oct 26 '18 at 08:36
  • @RobertKock My intent is to not duplicate code. Hiding `a` and `aa` in `B` and `C` works, but i would need to copy-paste code between the classes; – Escu Esculescu Oct 26 '18 at 08:39
  • @MauricePerry Think of `B` and `C` as being 2 types of `A`, each of which has different `a` and `aa` values, which are, in turn, the same for all `B`s and for all `C`s. This kind of spells `static` to me – Escu Esculescu Oct 26 '18 at 08:44
  • @PushpeshKumarRajwanshi Sorry about that. I changed it it now. – Escu Esculescu Oct 26 '18 at 08:45
  • 1
    @EscuEsculescu a static variable has only one instance. Period. a and aa are identical whether they're viewed from A, B or C. – Maurice Perry Oct 26 '18 at 08:49
  • 2
    This looks like [XY problem](https://meta.stackexchange.com/q/66377). The big questions here are "why did you made those variables static in the first place?" and "why are you setting them in constructor?". – Pshemo Oct 26 '18 at 08:54

3 Answers3

1

The short answer is you can't. By declaring them static you're basically saying "I only want one instance of this variable per JVM" (or more accurately: one per classloader), thereby removing any link to instances of this object.

There's a really good explanation in this answer: https://stackoverflow.com/a/797989/881976

Jeroen Steenbeeke
  • 3,884
  • 5
  • 17
  • 26
0

The word that you mentioned in your post "inherit" applies only when you create instance variables instead of static variables.

So the first thing you need to do is change your static variables to instance variables. That is the only way where you can create different instances of your B and C class and have their own copies of values.

Like you wrote,

"remain static, are still inherited from A"

This is purely contradictory. As long as they remain static, they can never be inherited.

Have a look at this answer. Static vs Instance Variables: Difference?

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
0

call to super(3,4) or super(2,3) is not possible since you dont have that constructor in those classes.

And if you call super you set values for variables of that class. like super(2,3) will set values for A , now for class B you can do any manipulation on the fields. Well overall i am not sure if the question is valid or not. here is what can be done well does not seem to be useful:

public class A {

 static int a, aa;
 A(){
     System.out.println("aa");
 }
    A(int i,int j) {

        this.a = i;
        this.aa = j;

    }
}

class B extends A {

    B(int i,int j) {
        super(i, j);
        this.a=i+1;
        this.aa=j+1;
        System.out.println("in b="+a+" "+aa);
    }
}

public class Test { public static void main(String[] args) {

        A a = new A(1,2);
         B b = new B(2,3);

    }

}

purvaB
  • 49
  • 4