Java | static vars in parent and child class | accessing the child var value from parent class

1.3k Views Asked by At

i have a few sub classes with unique static vars in this case called 'x'. All of those sub classes are using the static var the same way, so I want to reduce code duplication and put the functionality in the super class. In this case the method 'getX' in the super class. From here I want to return the value of x. Right now I'm facing the issue that it uses the x value of the super class and not the value of the child class. How can I access the x value of the sub class from the super class?

public class Playground {

  public static void main(String[] args) {
    Parent parent = new Parent();
    Child child = new Child();
    Child1 child1 = new Child1();

    System.out.println("Parent.x " + parent.x);
    System.out.println("child.x " + child.x);
    System.out.println("child.x " + child1.x);

    System.out.println("get x: " + parent.getX());
    System.out.println("get x: " + child.getX());
  }
}

class Parent {
  static String x = "static of parent";
  String y = "instance of parent";

  String getX() {
      return x;
  }
}

class Child extends Parent {
  static String x = "static of child";
  String y = "instance of child";
}

class Child1 extends Parent {
  static String x = "static of child1";
  String y = "instance of child";
}

This code prints out: Parent.x static of parent child.x static of child child.x static of child1 get x: static of parent get x: static of parent <-- here should be static of child

Hope someone can help me.

Cheers

5

There are 5 best solutions below

0
On

Add the getX method in the child class. The parent getX method points to the static variable of the parent class.

2
On

What you're trying is a violation of the basic OOP principle information hiding / encapulation.

No other class should know about how a class stores or treats its properties (aka variables or fields). This includes that clases should not have getter and setter (as suggested by @RosárioPereiraFernandes)

There is an exception to this rule when the class represents a data transfer object (DTO) which has (almost) no business logic and just provides access to structured data. DTOs shoould have getters and (less often) setters.

Other classes should provide methods with busines related names to modify their inner state (the values of their member variables):

class Car{
  private final int maxSpeedInMph = 100;
  private int speedInMph = 0;
  public void accellerateBy(int speedDiffInMph){
    if( maxSpeedInMph >=speedInMph + speedDiffInMph )
       speedInMph += speedDiffInMph;
    else 
       speedInMph = maxSpeedInMph ;
  }    
  public void accellerateTo(int newSpeedInMph){
    if( maxSpeedInMph >=  newSpeedInMph)
       speedInMph = newSpeedInMph;
    else 
       speedInMph = maxSpeedInMph ;
  }
  public void decellerateBy(int speedDiffInMph){
    if( 0<=speedInMph - speedDiffInMph )
       speedInMph += speedDiffInMph;
    else 
       speedInMph = 0;
  }
  public void emengencyBreak(){
    speedInMph = 0;
  }
  // you get the idea?
}
0
On

A solution could be to not have a static in the Parent, but an instance field.

class Parent{
    private final X x
    public Parent(X x){
        this.x = x;
    }

    public X getX() {
        return x;
    }
}

Then in your child, you still can have a static, but you pass it at the constructor

class Child extends Parent {
    static final X x = ....
    public Child() {
        super(x);
    }
}
0
On

As far as I know it is not possible to access child-variables from the parent-class.

But you can override String getX() in the child class

@Override
protected String getX() {        
    return x;
}

Then Java calls the "lowest" getX()method


I would code your example different:

public class Playground {

private static Parent parent = new Parent("static of parent");
private static Child child = new Child("static of child");
private static Child1 child1 = new Child1("static of child1");

  public class Playground {

private static Parent parent = new Parent("static of parent");
private static Child child = new Child("static of child");
private static Child child1 = new Child("static of child1");

  public static void main(String[] args) {


    System.out.println("Parent.x " + parent.getX());
    System.out.println("child.x " + child.getX());
    System.out.println("child.x " + child1.getX());

    System.out.println("get x: " + parent.getX());
    System.out.println("get x: " + child.getX());
  }
}

 class Parent {
     private String x;
     public Parent(String x) {
         this.x = x;

     }

     public String getX() {
          return this.x;
      }
 }

class Child extends Parent {
    protected Child(String x) {
        super(x);
    }  
}

-> this will bring the following output:

Parent.x static of parent
child.x static of child
child.x static of child1
get x: static of parent
get x: static of child

Just ask if you have questions

2
On

Try adding the getX method to the child. Like this:

class Child extends Parent {
  static String x = "static of child";
  String y = "instance of child";
  String getX() {
      return x;
  }
}