当前位置: 动力学知识库 > 问答 > 编程问答 >

class - Modifying a variable in the upper levels in Java

问题描述:

I need to change a variable in the upper class from the nested (I guess it could be called this way, I am new to java, I searched it but couldn't find it)

The code:

public class mainClass {

public static void main(String[] args) {

boolean variableToChange = false;

Timer myTimer = new Timer();

TimerTask myTimerTask= new TimerTask() {

public void run() {

if(variableToChange==false) { //it can read the variable

variableToChange = true; //but it can't change it?!

//it triggers and error here

}

}

};

myTimer.scheduleAtFixedRate(myTimerTask, 0, 100);

}

}

网友答案:

You can access, but cannot change variableToChange since it's inside an anonymous inner-class and declared outside it.

From JLS 8.1.3:

Any local variable, formal method parameter or exception handler parameter used but not declared in an inner class must be declared final. Any local variable, used but not declared in an inner class must be definitely assigned before the body of the inner class.

Variables declared outside an anonymous inner-class are considered final inside it. You are probably using Java 8 where where final can be implicit.

A "dirty" workaround (which isn't recommended due to synchronization issues, but works) is to declare:

final boolean[] variableToChange = new boolean[1];

And then you will be able to change it inside:

variableToChange[0] = true;
网友答案:

This is the solution:

package com.example;

import java.util.Timer;
import java.util.TimerTask;

public class mainClass {

    public static class MyBoolean {
        public boolean value;
    };

   public static void main(String[] args) {
      final MyBoolean variableToChange = new MyBoolean();
      Timer myTimer = new Timer();
      TimerTask myTimerTask= new TimerTask() {
         public void run() {
            if(variableToChange.value==false) {    //it can read the variable
                variableToChange.value = true;         //but it can't change it?! 
                                                //it triggers and error here
            }
         }
      };
      myTimer.scheduleAtFixedRate(myTimerTask, 0, 100);
   }
}

You have to declare variableToChange as final, to allow its modification in inner class. You cannot modify it, since you declare as final variable. So you need to encapsulate it in a object, and then modify its attributes.

网友答案:

Try:

public static boolean variableToChange = false;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

              Timer myTimer = new Timer();
              TimerTask myTimerTask= new TimerTask() {
                 public void run() {
                    if(mainClass .variableToChange==false) {    //it can read the variable
                        mainClass .variableToChange = true;         //but it can't change it?! 
                                                        //it triggers and error here
                    }
                 }
              };
              myTimer.scheduleAtFixedRate(myTimerTask, 0, 100);
           }

variable must be in class, not in method because you want to change it from the other object.

分享给朋友:
您可能感兴趣的文章:
随机阅读: