Introduction to the

In general, we create classes and interfaces one class at a time, one interface at a time, but sometimes, for convenience or for special reasons, Java doesn’t mind writing multiple classes and interfaces in a file, which leads to the inner classes and interfaces we’re going to talk about today.

The inner class

Let’s start with an inner class, which is the class defined within the class. A class within a class can be thought of as a property of the class. A property can be static or non-static. Inner classes can also be defined in class methods, plus anonymous classes, for a total of five inner classes.

Static inner class

We define a static class inside the class as follows:

@Slf4j
public class StaticInnerClass {

    static class Inner {
        void print(a) {
            log.info("Inner class is: " + this); }}public static void main(String[] args) {
        StaticInnerClass.Inner inner = newStaticInnerClass.Inner(); inner.print(); }}Copy the code

Because static variables can be accessed directly by class name, we use new Staticinnerclass.inner () to instantiate the Inner class.

Non-static inner class

Classes defined in class can also be non-static, as follows:

@Slf4j
public class InnerClass {

     class Inner {
        void print(a) {
            log.info("Inner class is: " + this); }}public static void main(String[] args) {
        InnerClass.Inner inner = newInnerClass().new Inner(); inner.print(); }}Copy the code

To access a class variable, instantiate the Inner and then instantiate the Inner class: new InnerClass().new Inner().

Notice that we need to use two new’s here.

Static method inner class

We can define a class in a static method that is essentially a variable in the method. The variable cannot be static. Let’s look at the following example:

@Slf4j
public class StaticMethodInnerClass {

    private static String x = "static x";

    public static void print(a) {
        class MyInner {
            public void printOuter(a) {
                log.info("x is " + x);
            }
        }
        MyInner i = new MyInner();
        i.printOuter();
    }

    public static void main(String[] args) { StaticMethodInnerClass.print(); }}Copy the code

Method that we cannot instantiate externally.

An inner class for a non-static method

The same non-static method can define an inner class:

@Slf4j
public class MethodInnerClass {

    private  String x = "non static x";

    public  void print(a) {
        class MyInner {
            public void printOuter(a) {
                log.info("x is " + x);
            }
        }
        MyInner i = new MyInner();
        i.printOuter();
    }

    public static void main(String[] args) {
        newMethodInnerClass().print(); }}Copy the code

Note that the external class needs to be instantiated before the call can proceed.

An anonymous class

And finally, anonymous classes, classes that are instantiated directly when needed. We’ve encountered anonymous classes a lot. For example, when building a SortedSet, we can pass in a custom Comparator. We can implement this using anonymous classes or we can use lambda expressions directly.

public class AnonymousClass {

    public static void main(String[] args) {
        SortedSet sortedSet1 = new ConcurrentSkipListSet(new Comparator(){
            @Override
            public int compare(Object o1, Object o2) {
                return 0; }}); SortedSet sortedSet2 =new ConcurrentSkipListSet((o1, o2) -> 0); }}Copy the code

The internal interface

An Inner Interface is an Interface defined within an Interface. The most common is Entry in a Map:

public interface Map<K.V> {
    interface Entry<K.V> {
        K getKey(a);
    }
Copy the code

The internal interface must be static, because the interface cannot be instantiated, so in order to access the interface within the interface, it must be static. If not specified, the default is static.

Let’s look at an implementation of this internal interface:

public class MapImpl implements Map.Entry{
    @Override
    public Object getKey(a) {
        return 0;
    }

    @Override
    public Object getValue(a) {
        return null;
    }

    @Override
    public Object setValue(Object value) {
        return null; }}Copy the code

conclusion

This article explains the implementation of five inner classes and the application of an inner interface. You can easily understand this by thinking of the inner class or interface as a variable.

The article example github.com/ddean2009/learn-java-base-9-to-20

Author: Flydean program stuff

Link to this article: www.flydean.com/inner-class…

Source: Flydean’s blog

Welcome to pay attention to my public number: procedures those things, more wonderful waiting for you!