Change boolean Values?

后端 未结 5 1757
礼貌的吻别
礼貌的吻别 2020-12-11 16:40

I have a question about boolean values in Java. Let\'s say I have a program like this:

boolean test = false;
...
foo(test)
foo2(test)

foo(Boolean test){
  t         


        
5条回答
  •  臣服心动
    2020-12-11 17:14

    Your foo method changed the value of test to true. It looks like what you want is to use instance variables for each function.

    boolean test = false;
    ...
    foo(test)
    foo2(test)
    
    foo(Boolean test){
      this.test = true;
    }
    foo2(Boolean test){
      if(this.test)
       //Doesn't go in here
    }
    

    This way, your method only changes the value of test inside of that method, but your public test parameter stays with a false value.

提交回复
热议问题