I'm trying to modify a value in a superclass from a subclass, and call that value from another subclass. For some reason when I do the value becomes null, or resets. I was working on a larger scale project, but ran into this problem and figured it'd be easier for everyone if I tested it in a smaller program. Here's what I've got:
public class Superclass { private int x; protected void setX(int n) { this.x = n; System.out.println(this.x); } protected int getX() { return this.x; } } public class Subclass extends Superclass { public void begin() { setX(55); Subclass2 subclass2 = new Subclass2(); subclass2.begin(); } } public class Subclass2 extends Superclass { public void begin() { System.out.println(getX()); } } public class Main { public static void main(String[] args) { Subclass subclass = new Subclass(); subclass.begin(); } } The superclass value successfully sets to 55, but when I call it from Subclass 2 it returns 0.
Thanks