Generally speaking, we create classes and interfaces as one class file, one interface file, but sometimes for convenience or for some special reason, java doesn't mind writing multiple classes and interfaces in one file. That's because of the internal classes and interfaces we're going to talk about today.
Let's start with the internal class, which is the class defined in the class.A class in a class can be viewed as an attribute of the class, either static or non-static.Internal classes can also be defined in the class's methods, plus anonymous classes, which have a total of five internal classes.
Static Internal Class
We define a static class inside the class, as follows:
[@Slf4j](https://my.oschina.net/slf4j) public class StaticInnerClass { static class Inner { void print() { log.info("Inner class is: " + this); } } public static void main(String[] args) { StaticInnerClass.Inner inner = new StaticInnerClass.Inner(); inner.print(); } }
Because the static variable can be accessed directly by the class name, we use the new StaticInnerClass.Inner() to instantiate the internal class.
Non-static Internal Class
Classes defined in a class can also be nonstatic, as follows:
[@Slf4j](https://my.oschina.net/slf4j) public class InnerClass { class Inner { void print() { log.info("Inner class is: " + this); } } public static void main(String[] args) { InnerClass.Inner inner = new InnerClass().new Inner(); inner.print(); } }
To access the variables of a class, you need to instantiate the external and internal classes: new InnerClass().new Inner().
Note that we need to use two new ones here.
Static Method Internal Class
We can define a class in a static method that is essentially equivalent to a variable in a method, which of course cannot be static.Let's look at the following examples:
[@Slf4j](https://my.oschina.net/slf4j) public class StaticMethodInnerClass { private static String x = "static x"; public static void print() { class MyInner { public void printOuter() { log.info("x is " + x); } } MyInner i = new MyInner(); i.printOuter(); } public static void main(String[] args) { StaticMethodInnerClass.print(); } }
Classes in methods cannot be externally instantiated.
Internal class of nonstatic methods
The same non-static method can also define internal classes:
[@Slf4j](https://my.oschina.net/slf4j) public class MethodInnerClass { private String x = "non static x"; public void print() { class MyInner { public void printOuter() { log.info("x is " + x); } } MyInner i = new MyInner(); i.printOuter(); } public static void main(String[] args) { new MethodInnerClass().print(); } }
Note that the external class needs to be instantiated before it can be called again.
Anonymous Class
Finally, anonymous classes, classes instantiated directly when needed.Anonymous classes have been encountered many times, for example, when building a SortedSet, you can pass in a custom Comparator, you can implement it using an anonymous class, or you can use a lambda expression directly.
public class AnonymousClass { public static void main(String[] args) { SortedSet sortedSet1 = new ConcurrentSkipListSet(new Comparator(){ [@Override](https://my.oschina.net/u/1162528) public int compare(Object o1, Object o2) { return 0; } }); SortedSet sortedSet2 = new ConcurrentSkipListSet((o1, o2) -> 0); } }Internal Interface
Inner Interface is an interface defined in an interface.The most common is Entry in Map:
public interface Map<K, V> { interface Entry<K, V> { K getKey(); }
The internal interface must be static here, because the interface cannot be instantiated, it must be defined as static in order to access the interface in the interface.If not specified, static is the default.
Let's look at an implementation of this internal interface:
public class MapImpl implements Map.Entry{ @Override public Object getKey() { return 0; } @Override public Object getValue() { return null; } @Override public Object setValue(Object value) { return null; } }summary
This paper explains the implementation of five internal classes and the application of one internal interface.As long as you consider the internal class or interface as a variable, you can have a good understanding of the above.
Examples of this article https://github.com/ddean2009/learn-java-base-9-to-20
Author: Fldean Programs
Links to this article: http://www.flydean.com/inner-class-inner-interface/
Source: Fldean's blog
Welcome to my Public Number: program stuff, more exciting waiting for you!