P. Barbra
P. Barbra

Reputation: 21

non-static global variable in java

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.

Upvotes: 0

Views: 3406

Answers (3)

SamTebbs33
SamTebbs33

Reputation: 5647

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

Upvotes: 1

Adrian Shum
Adrian Shum

Reputation: 40036

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();

Upvotes: 0

Nio Pullus
Nio Pullus

Reputation: 55

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;
    }

}

Upvotes: 1

Related Questions