access to variable within inner class in java

孤者浪人 提交于 2019-11-27 03:12:26

问题


I'm trying to create an array of JLabels, all of them should go invisible when clicked. The problem comes when trying to set up the mouse listener through an inner class that needs access to the iteration variable of the loop used to declare the labels. Code is self-explanatory:

    for(int i=1; i<label.length; i++) {
       label[i] = new JLabel("label " + i);
       label[i].addMouseListener(new MouseAdapter() {
          public void mouseClicked(MouseEvent me) {
             label[i].setVisible(false);   // compilation error here
          }
       });
       cpane.add(label[i]);
    }

I thought that I could overcome this by the use of this or maybe super instead of the call of label[i] within the inner method but I haven't been able to figure it out.

The compilation error is: local variable i is accessed from within inner class; needs to be declared final`

I'm sure that the answer must be something really silly I haven't thought of or maybe I'm making some small mistake.

Any help would be appreciated


回答1:


Your local variable must be final to be accessed from the inner (and anonymous) class.

You can change your code for something like this :

for (int i = 1; i < label.length; i++) {
    final JLabel currentLabel =new JLabel("label " + i); 
    currentLabel.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            currentLabel.setVisible(false);   // No more compilation error here
        }
    });
    label[i] = currentLabel;
}

From the JLS :

Any local variable, formal parameter, or exception 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 (§16) before the body of the inner class.


Resources :

  • JLS - Inner Classes and Enclosing Instances



回答2:


If you're having a problem accessing i, make another variable outside the scope of your inner-class (e.g. before label[i].addMouseListener(...)):

for(int i=1; i<label.length; i++) {
   label[i] = new JLabel("label " + i);

   final int localI = i;
   label[i].addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent me) {
         label[localI].setVisible(false);
      }
   });
   cpane.add(label[i]);
}



回答3:


you can also use getSource in your program. After this you can access your component with the help of typecasting. it will reduce extra lines of code, your code will look like this

for (int i = 1; i < label.length; i++) { 
   currentLabel.addMouseListener(new MouseAdapter(e) {
      public void mouseClicked(MouseEvent me) {
         JLabel label = (JLabel) me.getSource();
      }
   });
}



回答4:


This happens because label is not specified as final.

Declare the array of labels as:

final JLabel[] label;

instead of:

JLabel[] label;

Your MouseAdapter is not an inner class; it's an anonymous class. Anonymous classes can only refer to final variables of their enclosing code.




回答5:


Anonymous inner classes may only access variables of the enclosing method that are final.



来源:https://stackoverflow.com/questions/3901597/access-to-variable-within-inner-class-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!