Effective Java in the requirements of the class, interface, etc., summed up is enough to do, do not give too much access, the definition of the class is also, if a class is only used inside the class, then the class is defined as an inner class bar.
The inner class is divided into four types:
1, static inner class: Static inner class is in front of class more static keyword inner class, this class and class static method and static variable, for the class province to operate, in the static inner class can freely access the static method and static variables of its class.
2, non-static inner class: relative to the static inner class, in fact, everything defined inside the class is limited by the access modifier, so the non-static inner class and the class's Nonstatic method can access everything in its class, except that the meaning of this may change in the inner class.
3. Anonymous class: Anonymous class is a way to define classes when creating class objects, and it's a little difficult to describe them, and here's a simple example.
4, Local class: The local class and non-static inner classes are the same, the difference lies in their scope, the scope of non-static inner class is the entire class, then the scope of the local class is where it is.
Let's give an example of a non-static inner class:
PackageAlibaba; Public classInnerclass { Public inti; classHelloWorld { Public voidSetI (intk) {i=K; } } Public Static voidMain (string[] args) {//TODO auto-generated Method StubInnerclass IC =NewInnerclass (); HelloWorld HW= IC.NewHelloWorld (); Hw.seti (3); System.out.println (IC.I); }}
The example is simple, the result of printing is 3.
Here's an example of an anonymous class:
Interface PR { void print1 (); } Public class Nonameclass { public PR dest () { returnnew PR () { publicvoid print1 () { System.out.println ("Hello world!!" ); } }; }
The last object returned is an anonymous inner class.
But think of the internal class in peacetime to write code in the process of using is not much, just in the source of HashMap to see the non-static internal class entry, may be usually in the process of writing code did not pay attention to it.
Effective Java within class