0

Ok so I have 2 classes for instance:

public class Boolean {
    public static void test(boolean test1, boolean test2, boolean test3, boolean test 4, boolean test 5, boolean test 6){
        test1 = true;
        test2 = true;
        test3 = true;
        test4 = true;
        test5 = true;
        test6 = true;
    }
}
    

public class Test {
    private boolean test1 = false;
    private boolean test2 = false;
    private boolean test3 = false;
    private boolean test4 = false;
    private boolean test5 = false;
    private boolean test6 = false;

    public static void main(String[] args) {
        Boolean.test(test1, test2, test3, test4, test6, test7);
        //I want test1,...,test6 to be true now.
    }

}

(This is just a simple example code to describe my problem, obviously this could be solved easier)

So I tried this apporoach for one of my projects however it does not seem to work. Any ideas why this wont work?

Mendy
  • 15
  • 2
  • The simple answer is that this just isn't how Java works and you'll either need to pass in a mutable object or return a collection and update in the caller. – Dave Newton Jan 13 '22 at 17:33
  • yeah, kind of.. how would I solve that issue then? Getter and setters? or is there a better solution? – Mendy Jan 13 '22 at 17:35
  • An object containing those values could be changed by the called function. That said: that kind of mutability can lead to confusing code; it's often easier to reason about if there's a returned object containing the updated values. But there's no single "right" way and what makes the most sense is often context-dependent. – Dave Newton Jan 13 '22 at 17:43

0 Answers0