How the private properties of inner class are accessed from outside class?

冷暖自知 提交于 2019-12-06 14:17:45

问题


I have read this concept in respect to static inner class : ViewHolder declared as inner class inside the adapter of ListView to enhance the performance of getView().

Consider the below class

public class OuterClass{

    public class InnerClass{
        private int privateProperty= -2;
    }

    public static void main(String[] args) {
        OuterClass oc = new OuterClass();
        InnerClass ic = oc.new InnerClass();
        ic.privateProperty = -98;
    }
}

If inner class contains private properties and an object of inner class is created inside a method of outer class then the inner class private properties can be accessed directly using . 'dot' operator.

I have read somewhere that the private properties of the inner class are accessed using synthetic setter getter methods from outer class

I want to clear my concept regarding the same.


回答1:


The compiler generates method to access private members of an inner class. If you compile your example code and examine the bytecode, you will find that it is as if it were written like this:

public class OuterClass{

    public class InnerClass{
        private int privateProperty= -2;
        static int access$002(InnerClass obj, int value) {
            obj.privateProperty = value;
            return value;
        }
    }

    public static void main(String[] args) {
        OuterClass oc = new OuterClass();
        InnerClass ic = oc.new InnerClass();
        InnerClass.access$002(ic, -98);
    }
}

This conversion of the line

ic.privateProperty = -98;

into the method call:

InnerClass.access$002(ic, -98);

together with the creation of the static method InnerClass.access$002 is done by the compiler. The static method (named access$002 by my compiler) is an example of a "synthetic setter method" you have read about. As a result, the bytecode for the two classes do not violate Java's access rules.




回答2:


Your concept is wrong.. Inner classes are meant to use inside the container classes only, This idea coming from the concept that you don't want to expose unnecessery classes to the developer, Which is not relevant to all of the project.

In this case InnerClass will be related to only to OuterClass. In the main you should create new only to OuterClasS and the OuterClass will create instance of InnerClass

So it should be something like this:

public class OuterClass{
private InnerClass in;

public Class OuterClass() {
    in = new InnerClass();
}
//getters & setters
public void setInnerProperty(int x) {
    in.setPrivateProperty(x);
}

public class InnerClass{
    private int privateProperty= -2;
    //getters & setters
}

public static void main(String[] args) {
    OuterClass oc = new OuterClass();
    oc.setInnerProperty(98);
}
}

In case you want to change it from the main.. This is the way to do it, but not recomended.

Hope that helps



来源:https://stackoverflow.com/questions/24311942/how-the-private-properties-of-inner-class-are-accessed-from-outside-class

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