What inner class?
It is seen from the beginning of the article, defined as inner classes: class definition of another class or method. According to different usage scenarios, inner classes can be divided into four categories: internal class members, local inner classes, anonymous inner classes and static inner classes. Each features and considerations are different, let them out.
Members of the inner class
As the name suggests, the internal members of the class is defined within a class, as a member of the class class. as follows:
public class Outer { public class Inner{ } }
Features are as follows:
- Members of the class may be internal authority modifier (EG.
public,private等
) The modified - Members of the inner class can access all members of the outer class, (including
private
) a member of - Members of the inner class is the default class contains a pointer to an external object reference
- As the use of
this
the same, when the member name or method name occur coverage, you can use the name of the outer class plus .this specify access outside the class members. Such as:Outer.this.name
- Members of the inner class can not define the
static
members of the - Members of the inner class created syntax:
Outer outer=new Outer(); Outer.Inner inner=outer.new Inner();
Partial inner class
Internal local class or a class defined in the method scope, the difference between it and the inner member classes differ only access.
public class Outer{ public void test(){ class Inner{ } } }
Features are as follows:
- Local inner classes can not have access modifiers
- Partial internal class can not be defined as
static
- Partial internal class can not be defined
static
members - Partial internal default class comprises a reference to an external class object
- You can also use the local inner class
Outer.this
syntax to access external development of class members -
Want to use the local variables inside the class or domain method, the variable must be
final
aAfter JDK1.8, no
final
modification,effectively final
can be. What does that mean? It is notfinal
modified, but if you addfinal
the compiler can not complain.
Anonymous inner classes
No name anonymous inner classes are merged together and inheritance within the class
public class Outer{ public List<String> list=new ArrayList<String>(){ { add("test"); } }; }
This is usually the most common syntax.
Features an anonymous inner classes are as follows:
- Anonymous inner classes of the blocks represent separate initialization block
{}
- Anonymous inner classes want to use the method or variable domain, the variable must be
final
modified, after JDK1.8effectively final
can - Anonymous inner classes included by default external object references
- Anonymous inner class represents a succession of dependent class
Static inner classes
Static inner classes with static
modified internal member class
public class Outer { public static class Inner{ } }
Features are as follows:
- Static inner class is an internal class only four types does not contain a reference to the external object
- Static inner class defines
static
members -
Nested classes can access any static data members and methods of the outer class.