inner class non-final variable java

前端 未结 4 1538
滥情空心
滥情空心 2021-02-15 14:59

I needed to change variables inside an inner class and I got the infamous \"Cannot refer to a non-final variable inside an inner class defined in a different method\" error.

相关标签:
4条回答
  • 2021-02-15 15:05

    I posted a similar answer in my other thread here. Basically the idea is to create a "wrapper" which wraps over pretty much any Object type. Since final in Java stands for "no reassignment" and not "constant", this trick pretty much works out fine. But as mentioned in the original post, make sure you tread with caution when using it in a multi-threaded environment.

    0 讨论(0)
  • 2021-02-15 15:06

    You can simply create an inner class instead of an anonymous one (like you are currently doing). Then you have a constructor and any other methods you want to set your members. No hackiness required (like the array of 1 case).

    I find this cleaner if the class requires any exchange of data with its outer class, but admit it is a personal preference. The array of 1 idiom will work as well and is more terse, but frankly, it just looks fugly. I typically limit anonymous inner classes to those that just perform actions without trying to update data in the outer class.

    For example:

    private MyListener listener = new MyListener();
    
    void onStart(){
      bt.setOnClickListener(listener);
    }
    
    class MyListener implements OnClickListener
    { 
        String name;
        int value;
    
        void setName(String newName)
        {
            name = newName;
        }
    
        void setValue(int newValue)
        {
            value = newValue;
        }
    
        public void onClick(View v) 
        {
            // Use the data for some unknown purpose
        }
    }
    

    If there are multiple threads involved, then appropriate synchronization will have to be used as well.

    0 讨论(0)
  • 2021-02-15 15:13

    Since you appear to be setting multiple things (from the comments), make a method in the main class, button1WasClicked(), (a better name might be doUpdate, doSave etc. - something relevant to what the button does), put the proper code there, and call it from the inner class / listener. (If you are using Swing I'd make it an Action, YMMV)

    That way if later on there is a menu or an intent or a gesture that needs to execute the same stuff, the call is there.

    0 讨论(0)
  • 2021-02-15 15:19

    I would keep a reference to your on click listener in the outer class, and make the int a member variable in your listener. Just store the variable in the listener on click, then grab the variable in the outer class when you need it, rather than setting it at the point of the click.

    To put it simply, if the inner class needs to change it, make it a variable in the inner class.

    0 讨论(0)
提交回复
热议问题