Home » Java » java – How to access and modify variable stored at another class-Exceptionshub

java – How to access and modify variable stored at another class-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to implement something related to a boolean.
Let’s say I have 3 classes.

Class 1:

public boolean haha

public boolean getBoolean()
{return haha;}

public void changeBoolean()
{if(haha == true){
haha = false;}
else if (haha == false){
haha = true;}
}

My question is: How do i implement:

Class 2:

private int a
private int b
public void method1(){
if(haha == true){
a = 1;} else if (haha == false){
a = 2;}
} 

public void change(){}

I dont know how to write the condition of the if statement to code, i want it to access the boolean of class 1.

I also need the change method to execute changeBoolean method in class 1 but i dont know how to write it. I need it to change the boolean haha stored in class 1.

Class 2 can change the boolean state. (i want this state to be global and if possible stored at class 1 so that class 3 can access it as well)

Class 3:

private int c
private int d
public void method2(){
if(haha == true){
c = 1;} else if (haha == false){
c = 2;}
}

Again, in class 3, I dont know how to write the if statement so it access the boolean in class 1.

It means when haha is true, and i change it to false (changed with method in class 2), when class 3 access the boolean it has to be false as well
(but i dont know how to write code to access the boolean and change the boolean that is stored in class 1)

How to&Answers:

There are various ways in achieving what you want and without going into too much details as this is basic stuff and information is easily available, I’ll just show you one of these to get you started and leave the rest for you to discover.

Live demo

public static void main(String[] args) {
    // Create an A instance with a Flag instance as constructor argument,
    // value set to 2 by constructor call to an A class method
    A a = new A(new Flag(false));

    // Create a B instance with -1 initial value
    B b = new B(-1);

    // Call a method on "b" with an A instance as argument, 
    // updating the state of "b" (value from -1 to 2)
    b.method(a);

    // Call a method on "a" which internally modifies the state 
    // of the Flag instance in "a"
    a.change();

    // Call a method on "a", updating state of "a" (value from 2 to 1)
    a.method();

    // Call a method on "b" with an A instance as argument, 
    // updating state of "b" (value from 2 to 1)
    b.method(a);
}

public class Flag {
    private boolean value;

    public Flag(boolean value) {
        this.value = value;
    }

    public boolean get() {
        return value;
    }

    public void toggle() {
        value = !value;
    }
}

public class A {
    private Flag flag;
    private int value;

    public A(Flag flag) {
        this.flag = flag;

        // Initially setting "value" depending on the state of the passed "flag" argument
        method();
    }

    public void method() {
        if (flag.get()) {
            value = 1;
        } else {
            value = 2;
        }
    }

    public int getValue() {
        return value;
    }

    public void change() {
        flag.toggle();
    }

    // returns "flag"'s field current status
    public boolean flagStatus() {
        return flag.get();
    }
}

public class B {
    private int value; // updates only by method(A a) invocation

    public B(int value) {
        this.value = value;
    }

    public void method(A a) {
        if (a.flagStatus()) {
            value = 1;
        } else {
            value = 2;
        }
    }

    public void method(Flag flag) {
        if (flag.get()) {
            value = 1;
        } else {
            value = 2;
        }
    }

    public int getValue() {
        return value;
    }
}

Many changes could be made to the example above and I would encourage you to play with ideas like:

  • modify both A#getValue() and B#getValue() methods to check flag status and update their values first thus removing the need of manual update in main()

  • modify B to have a filed with the same Flag instance as A (but without the method in A which modifies the flag), which is internally used to update its value or to have an A field (a instance passed as constructor argument), thus eliminating the need to pass an A instance each time when an update is required