简体   繁体   中英

How do I change the value of a boolean within a method - Java?

I read in a book that when you change the value of a method parameter that's a boolean or other basic datatype within the method it only is changed within the method and remains the same outside. I want to know if there is some way for me to actually change it within the method. For example:

public class Change {

    void convert(boolean x, boolean y, boolean z) { //i want to set x,y, and z to false in this 
        x = false;
        y = false;
        z = false;
     }

}

//Now in my main class: 
public static void main(String[] args) {
    boolean part1 = true;
    boolean part2 = true;
    boolean part3 = true;
    System.out.println(part1 + " " + part2 + " " + part3);


    Change myChange = new Change();
    myChange.convert(part1,part2,part3);
    System.out.println(part1 + " " + part2 + " " + part3);

}

EDIT1: These answers were good but not quite what i want to acheive. I want to put in part1, part2, and part3 when i call the method than i want them to be set to false within the method. The specific reason i asked this question was because im trying to code a battleship and i have a subroutine class with a method that when its called it checks if a ship has been sunk. If the there was a sink than the method will set a ton of boolean variables to false.

EDIT2: Just to clarify, I want something like this:

void convert(thing1,thing2,thing3,thing4) {

     //some code here that sets thing1,thing2,thing3, and thing4 to false

 }

 // than in main:
 boolean test1 = true;
 boolean test2 = true;
 boolean test3 = true;
 boolean test4 = true;
 convert(test1,test2,test3,test4);
 System.out.println(test1 + " " + test2 + "....");
 //and that should print out false, false, false, false

You can do it with this methodology

// these are called instance variables
private boolean x = false;
private boolean y = false;
private boolean z = false;

// this is a setter
public static void changeBool(boolean x, boolean y, boolean z) {
    this.x = x;
    this.y = y;
    this.z = z;
}

Call the method like this

changeBool(true, true, false);

The values for x, y, and z are now changed.

This is a common problem in Java - pass-by-value vs. pass-by-reference. Java is always pass-by-value, where you're thinking of it as pass-by-reference.

Like @Rafael has said, you need to use instance variables to do what you want. I've gone a bit further and edited your source code to do what you want:

public class Change {
boolean part1;
boolean part2;
boolean part3;

Change(boolean x, boolean y, boolean z) {
    part1 = x;
    part2 = y;
    part3 = z;
}

void convert(boolean x, boolean y, boolean z) { //this now sets the class variables to whatever you pass into the method 
    part1 = x;
    part2 = y;
    part3 = z;
}

// Now in my main class:
public static void main(String[] args) {
    Change myChange = new Change(true, true, true);
    System.out.println(myChange.part1 + " " + myChange.part2 + " "
            + myChange.part3);
    myChange.convert(false, false, false);
    System.out.println(myChange.part1 + " " + myChange.part2 + " "
            + myChange.part3);

}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM