0

Is there any way to create a variable which i can change in class A and by these changes affect what will happen in another class B. Hope you understand.

Something like this:

class A{
public int var = 0;

}

And use value of variable var like this:

class B{
if(var == 0)
   {
    System.out.println("right now, var is equal 0");
   }
else if(var == 1)
   {
    System.out.println("right now, var is equal 1");
   }
}

Also as you can see, var can't be static because i need to change it's value during run of app.

8
  • Not possible unless B has a reference to a (shared?) instance of A. But why wouldn't you use static for a global variable? And of course the requisite "why are you using global variables?" Commented May 17, 2016 at 1:16
  • 3
    There is nothing that prevents you from changing the value of a static variable during runtime. Commented May 17, 2016 at 1:16
  • 3
    Look up observer design pattern, because it looks like that's what you want -- to be notified of changes in another object. Commented May 17, 2016 at 1:17
  • 1
    'Non-static global' is a contradiction in terms, and there are no global variables in Java, which makes your question doubly meaningless. Commented May 17, 2016 at 1:41
  • 2
    I do not get why you say "var can't be static because i need to change it's value during run of app". Static is effectively same as an instance variable. Only difference is, the "instance" here is a class object. As long as it is not final, you can change it in the runtime. Commented May 17, 2016 at 1:45

3 Answers 3

1

I would do it by having a reference of class B in class A so that A could also change B when it needed to. For example:

public class A {

    private int var;
    private B b;

    public A(B b) {
        this.var = 0;
        this.b = b;
    }

    public void set(final int var) {
        this.var = var;
        b.set(var);
    }

}

public class B {

    private int var = 0;

    public void set(final int var) {
        this.var = var;
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

@SamTebbs33 Could you please guys use my System.out.println() so I can understand what which class does. According my question.
1

You can use the observer-observed pattern.

public class A extends Observable {

    private int var = 0;

    public void setVar(int val) {
        this.var = val;
        notifyObservers();
    }

}

public class B implements Observer {

    public void init(A a) {
        a.addObserver(this);
    }

    @Override
    public void onUpdate(Observable obs, Object arg) {
        // Do something when A is updated
        if(var == 0) System.out.println("right now, var is equal 0");
        else if(var == 1) System.out.println("right now, var is equal 1");
    }

}

The way it works is that A becomes Observable, which means that other classes can be updated when something changes in A (the other classes are notified by A calling notifyObservers()). The onUpdate() method in the observer is then called with the Observable (here A) as the first argument. If you call notifyObservers() with an Object argument, the Object argument in onUpdate() will use that value.

Here is the control flow:

A : setVar -> A : notifyObservers -> B : onUpdate

3 Comments

Now that's what I'm talkin' about. 1+
@SamTebbs33 Thanks for your solution. Is there any way to implement this problem? Cant extend my class of two classes.
@P.Barbra Well I have given you an example implementation here, you will need to adapt it to your own needs. Edit: I have added the code from your question to onUpdate() in B
0

Although Observer pattern suggested by other answer is actually a cleaner approach, given that OP seems don't even have proper understanding on basic concepts of Java as an OOP (e.g. what an object instance is), I believe what he is looking for is something even more basic, which is a reference to another object:

class A {
    private int value = 0;

    // getters and setters 
}

class B {
    private A a;

    // using constructor to have object reference populated
    // is only ONE OF THE WAYS
    public B(A a) {
        this.a = a;
    }

    public void foo() {
        System.out.println("my referred A value " + this.a.getValue();
    }
}

Of course you need to properly construct them. Somewhere in your code:

A a = new A();
B b = new B(a);
b.foo();
a.setValue(100);
b.foo();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.