unlimited !


public class Outer2 {
        private int size;

        public Object makeTheInner(final int localVar) {
                final int finalLocalVar = 6;
                class Inner {
                        public String toString() {
                            return ("#< Inner size=" 
+ size +" localVar=" + localVar 
+" finalLocalVar=" + finalLocalVar + " >");
                        }
                }

                return new Inner();
        }

        public class Inner {
                public void doStuff() {
                        size++;
                }
        }
}

测试此类:

public class TestInner {
        public static void main(String[] args) {
                Outer2 outer = new Outer2();
                Object obj = outer.makeTheInner(1);
                // Must create an Inner object relative to an Outer
                Outer2.Inner inner = outer.new Inner();
                inner.doStuff();
                System.out.println(obj.toString());
        }
}

result:
#< Inner size=1 localVar=1 finalLocalVar=6 >
Advertisements

Author: lwpro2

Java J2EE professional

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s