Access variable from inner class without making it final

℡╲_俬逩灬. 提交于 2019-12-04 22:48:41

If you declare them as member variables (outside of a method and preferably before onCreate() for readibility) they will be accessible throughout the Activity even to inner classes of the Activity

No,Not possible.

JLS # chapter 8

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.

As a side note:Make use of Collections instead of declaring that many fields.

Here is how can you access them example from docs:

class WithDeepNesting {
    boolean toBe;
    WithDeepNesting(boolean b) { toBe = b; }

    class Nested {
        boolean theQuestion;
        class DeeplyNested {
            DeeplyNested(){
                theQuestion = toBe || !toBe;  // here you are able to access now.
            }
        }
    }
}

The usual approach is to convert them into member variables. However, in your case, you need to rethink your design.

You have way too much code duplication. You should refactor it to use a collection, at which point the inner class problem solves itself.

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