3

In C++ I could use 1 class throughout multiple files and when I modify a value using a function from one file, it would change that value globally.

In Java every file has to have it's own class; when I change a value using a method from one file, it does not change this value globally.

For example in C++ the files might look like this:

someClass.h

class someClass{
private:
    int x;
public:
    //Constructor
    someClass(){
        x = 5;
    }
    void changeVariable(); //Declaring function for later
}

main.cpp

int main(){
    someClass cClass;
    cClass.changeVariable(); //Call function
    cout << x; //outputs 6
}

fileA.cpp

void someClass::changeVariable(){
    x = 6; //x is changed to 6 globally.
}

In Java:

someClass.java

public class someClass {
    int x;

    //Constructor
    public someClass() {
        x = 5;
    }

main.java

public class main {
    public static void main() {
        someClass cClass = new someClass();
        subClass cSub = new subClass();
        cSub.changeVariable();
        System.out.print(x); //outputs 5
    }
}

fileA.java

public class fileA extends someClass {
    void changeVariable() {
        x = 6; //x is changed to 6 only for fileA
    }
}

My question is how can I change a variable from the sub class so that the variable is changed globally (For Java). Sorry if the question is still confusing.

Monsterjamp
  • 135
  • 1
  • 8
  • expose a function. setXyz – tgkprog May 02 '15 at 18:58
  • What do you need? Why do you need to do it that way? – justanothercoder May 02 '15 at 19:00
  • am I mistaken that someClass::x = 6; should work? – rfreytag May 02 '15 at 19:01
  • `super.x = newVariable` – hyper-neutrino May 02 '15 at 19:11
  • Your C++ example has nothing to do with inheritance. In C++ the declaration of a member function and its definition can be separated in different files - this is what you have done, so the equivalent example in Java would be nearly the same like your `someClass` in C++, except that you have to define the `changeVariable()` function where you declare it. (i.e. `void changeVariable() { x=6; }) – tomse May 02 '15 at 19:53
  • @tomse Well my question wasn't a question about inheritance in Java, it was more of a question of using a class throughout multiple files. It turns out using abstract classes is the closest equivalent to my C++ example. – Monsterjamp May 02 '15 at 20:35
  • @Monsterjamp The closest equivalent to your C++ eample is just inlining the definition of `changeVariable()` in your `someClass` it has nothing to do with abstract / inheritance or static. But maybe your goal is a different one and if it works for you - OK. – tomse May 02 '15 at 20:53
  • You might want to look at the 'singleton' type of class. There is only one of these and all instances have the same values. – cliff2310 May 02 '15 at 22:18

3 Answers3

6

Try with this:

public class someClass {
    static int x;

    //Constructor
    public someClass() {
        x = 5;
    }

That a variable where static means that its value is common for all objects of the same class. That only one variable x is created for all, not one for each object.

Read that answer if you want a good explication of what static means:

What does the 'static' keyword do in a class?

Community
  • 1
  • 1
Shondeslitch
  • 1,049
  • 1
  • 13
  • 26
1
Expose a function. setXyz in parent

public class someClass {
    int x;
 //Constructor
    public someClass() {
        x = 5;
    }
    public void setX(int n){
         this.x = n;
     }
  } 

   public class fileA extends someClass {
    void changeVariable() {
      setX(6); 
   }
tgkprog
  • 4,493
  • 4
  • 41
  • 70
1

No need for static variable, this is the most equivalent thing you can do in Java:

public abstract class SomeClass {
  protected int x = 5;
  public abstract void changeVariable();
}

public class FileA extends SomeClass {
  @Override
  public void changeVariable() {
    x = 6;
  }
}

SomeClass doesn't have to be abstract of course, but you would have to implement it's changeVariable method.

Also x cannot be private, it has to be protected, so that it can be accessed by subclasses.

kajacx
  • 12,361
  • 5
  • 43
  • 70
  • Your answer seems to be closer to what I was looking for but is it common to use abstract classes in Java. – Monsterjamp May 02 '15 at 20:37
  • @Monsterjamp I personally don't use abstract classes so much, not sure how about other programmers. IMO In standard library interfaces seem to be more common. However abstract classes saved my life a few times and they can simply your code quite a lot. – kajacx May 02 '15 at 22:02