摘自http://developer.android.com/intl/zh-cn/training/articles/perf-tips.html#PackageInner Consider the following class definition:

public class Foo {
    private class Inner {
        void stuff() {
            Foo.this.doStuff(Foo.this.mValue);
        }
    }

    private int mValue;

    public void run() {
        Inner in = new Inner();
        mValue = 27;
        in.stuff();
    }

    private void doStuff(int value) {
        System.out.println("Value is " + value);
    }
}

What’s important here is that we define a private inner class (Foo$Inner) that directly accesses a private method and a private instance field in the outer class. This is legal, and the code prints “Value is 27” as expected.

The problem is that the VM considers direct access to Foo’s private members from Foo$Inner to be illegal because Foo and Foo$Inner are different classes, even though the Java language allows an inner class to access an outer class’ private members. To bridge the gap, the compiler generates a couple of synthetic methods:

/*package*/ static int Foo.access$100(Foo foo) {
    return foo.mValue;
}
/*package*/ static void Foo.access$200(Foo foo, int value) {
    foo.doStuff(value);
}

The inner class code calls these static methods whenever it needs to access the mValue field or invoke the doStuff() method in the outer class. What this means is that the code above really boils down to a case where you’re accessing member fields through accessor methods. Earlier we talked about how accessors are slower than direct field accesses, so this is an example of a certain language idiom resulting in an “invisible” performance hit.

If you’re using code like this in a performance hotspot, you can avoid the overhead by declaring fields and methods accessed by inner classes to have package access, rather than private access. Unfortunately this means the fields can be accessed directly by other classes in the same package, so you shouldn’t use this in public API.

也就是说在内部类(与内部类声明的访问权限无关)中对宿主类的private成员的访问时,会生成 static 的 access$xxx 方法来对宿主类的私有成员的访问做桥接。从而会增加类的方法数以及调用的开销。
解决办法是,将内部类需要访问的成员变量或方法的访问权限声明>=package,也就是非private即可。


分享到