4

Let's say I have a public class with static methods, one of them for example:

 public static void test(boolean b){
   b = !b;
 }

Let's say this class name is Test. From another class, where I have a variable boolean a = false, I call

 Test.test(a);

How can I make it change a permanently, and not just change it in that static methods scope?

Greyshack
  • 1,901
  • 7
  • 29
  • 48
  • 2
    Can you explain why you are trying to do this as it is likely there is a better way to solve your actual problem. – Peter Lawrey Dec 27 '14 at 19:54
  • 2
    I know how to go around it, I just didn't know if this is possible so I felt like asking :) – Greyshack Dec 27 '14 at 19:57
  • Then the answer is don't do that, do something else. Changing code on the fly is not a simple process. You should think about changing fields. – Peter Lawrey Dec 27 '14 at 20:21
  • Remember, functions don't receive the variables you pass into them; they receive the values of those variables. They can change the value they stored, but they can't change the variable. See 'Is Java “pass-by-reference” or “pass-by-value”?' – Vitruvie Dec 27 '14 at 20:34

6 Answers6

2

The only way to make the change permanent is to let the method have a return value and assign it to the variable :

public static boolean test(boolean b){
   return !b;
}

a = Test.test(a);
Eran
  • 387,369
  • 54
  • 702
  • 768
  • You sure it's the only way? How could I change two variables at once? – Greyshack Dec 27 '14 at 19:52
  • 1
    @Greyshack: You don't. If you need multiple variables to change, you should almost certainly encapsulate the combination of those variables in a class. – Jon Skeet Dec 27 '14 at 19:53
  • 1
    @Greyshack Changing two variables at once is not possible. If the variable is a reference to a mutable object, you can change the properties of the object, but you can't change the reference itself. – Eran Dec 27 '14 at 19:57
0

Use a static field:

public static boolean flag;

public static void test(boolean b){
    flag = !b;
}

Then:

boolean a = true;
Test.test(a);

System.out.println( Test.flag); // false
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

in your Test class you can define the boolean variable as static

public static boolean a;

and outside the class change or access it using Test.a=false; or a=Test.a

and if you need to use methods you can hide the static method with inheritance:

public class HideStatic {     
public static void main(String...args){  
    BaseA base = new ChildB();  
    base.someMethod();        
}  
}  

class BaseA {         
    public static void someMethod(){  
        System.out.println("Parent method");  
    }  
}  

class ChildB extends BaseA{  
    public void someMethod(){  
        System.out.println("Child method");  
    }  
}
void
  • 7,760
  • 3
  • 25
  • 43
  • Ok but that way I have the variable in the class with static methods, and what if I want to have different variables in different classes and use that method? – Greyshack Dec 27 '14 at 19:56
  • so what about hiding them using a new class which inherits the static holder class? (I edited the answer) – void Dec 27 '14 at 20:05
0

You can pass an instance to method and use setters to change more variables at once.

public static void updateData(MyClass instance) {
    instance.setX(1);
    instance.setY(2);
}
tomasbedrich
  • 1,350
  • 18
  • 26
0

I think you are asking for call by reference. You can get this in Java by using arrays:

public static void test(boolean[] b){
b[0] = !b[0];
}

boolean[] param = new boolean[] {a};
test(param);
a=param[0];
//a changed

This works, but it is ugly. If you need to return more than one value, have a look at the Pair or Tuple structures.

Community
  • 1
  • 1
Cfx
  • 2,272
  • 2
  • 15
  • 21
0

Sounds to me like you are looking for a Mutable Boolean, the simplest of which is an AtomicBoolean.

private void changeIt(AtomicBoolean b) {
    b.set(!b.get());
}

public void test() {
    AtomicBoolean b = new AtomicBoolean(false);
    changeIt(b);
    System.out.println(b);
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213