start

When it comes to the word inner class, I think many people have a feeling that they are familiar with it but not familiar with it. In my memory, some event monitoring in Java Swing module is mostly realized by internal class, and Spring JdbcTemplate also seems to be involved, it is not clear. Since inner classes are rarely used in most development, this is just a summary study.

The inner class

In Java, a class can be defined inside another class or a method. Such classes are called “inner classes.” Inner classes in the broad sense generally include these four types: ordinary inner classes (member inner classes), local inner classes, static inner classes, and anonymous inner classes.

Ordinary inner classes (Member inner classes)

A normal inner class is the simplest and most common inner class, as shown in the following code:

class Canvas {
    
    class Draw {}}Copy the code

In this way, the Draw class looks like a member of the Canvas class, which is called the outer class, so Draw is the inner class. So a normal inner class is called a member inner class. The member inner class has unconditional access to all member attributes and methods of the external class (private and static), as shown in the following code:

class Canvas {
    private int width = 100;
    public static int counts = 1;

    public void test(a) {// External class methods
        System.out.println("Canvas - test()");
    }

    class Draw {/ / inner classes
        public void drawThing(a) {
            System.out.println(width);      // Private member of the external class
            System.out.println(counts);     // Static member of the external class
            test();                         // External class methods}}}Copy the code

Note, however, that external class invalidation occurs when an inner class has a member variable or method with the same name as the inner class. That is, members of the inner class are accessed by default. If you want to access a member of an external class with the same name, you need to access it in the following form:

External class. this. member variable external class. this. member methodCopy the code

While an inner class can access an inner class member unconditionally, an outer class must first create an inner class object and then use a reference to that object to access it, as shown in this code:

class Canvas {
    private int width = 100;
    public static int counts = 1;

    public void test(a) {// External class methods
        System.out.println("Canvas - test()");
    }
    
    public void test2(a) {// Method 2 of the external class
        // This is a method of the outer class that needs an instance of the inner class to call the inner method
        Draw draw = new Draw();
        draw.drawThing();
    }

    class Draw {/ / inner classes
        public void drawThing(a) { System.out.println(width); System.out.println(counts); test(); }}}Copy the code

Member inner classes exist in external classes. To create instances of member inner classes, you need an instance of the external class. See the code below:

public class Test {
    public static void main(String[] args) {
        Canvas canvas = new Canvas();           // Get the external Canvas instance
        Canvas.Draw draw = canvas.new Draw();   // Create an inner Draw instance from the external Canvas instance}}Copy the code

Inner classes can be modified with private, public, protected, and package access. In the example above, if the member inner class Draw is decorated with private, it can only be accessed inside the outer class. If the member inner class Draw is decorated with public, it can be accessed anywhere. If protected, it can only be accessed under the same package or if it inherits from an external class. If it is the default access permission, it can only be accessed under the same package. This is a bit different from external classes, which can only be qualified with public and default access permissions.

Local inner class (method inner class)

From the above line, local inner classes are also called method inner classes, which you can roughly understand to mean that inner classes are defined inside outer class methods. In plain English, a local inner class is a class defined in a method or scope. Access to a local inner class is limited to the method or scope. See the code below:

class Canvas {

    public void test(a) {// External class methods
        class Draw {// Define an inner class in an outer class method}}}Copy the code

Note that a local inner class, like a local variable inside a method, cannot have public, protected, private, or static modifiers.

Static inner class

class Canvas {
    
    static class Draw {}}public class Test {
    public static void main(String[] args) {
        Canvas.Draw draw = newCanvas.Draw(); }}Copy the code

A static inner class is a class defined in another class with the keyword static in front of it. A static inner class does not depend on an external class, much like a static member property of a class, and it cannot use non-static member variables or methods of an external class. This includes the static keyword. If you are interested, use the static keyword in Java

Technically, static inner classes should not be inner classes. Because inner classes share a special relationship with outer classes, or rather, with instances. Static inner classes have no such relationship. It is simply located inside another class, so it can also be called a “nested class.”

Anonymous inner class

Anonymous inner classes are often used when writing code, as shown in Swing:

button.addActionListener(  
    new ActionListener() {  
        public void actionPerformed(ActionEvent e) {  
            System.out.println("You clicked the button."); }});Copy the code

The new ActionListener() in this code is the anonymous inner class, which is obviously new without an instance name.

Next, let’s continue to subdivide the anonymous inner class.

Inherited anonymous inner class
public class Canvas {

    public void doDraw(a) {
        System.out.println("draw one line");
    }

    public static void main(String[] args) {
        Canvas canvas = new Canvas(){
            public void doDraw(a) {
                System.out.println("draw many line"); }}; canvas.doDraw();// draw many lines}}Copy the code
Interface anonymous inner class
public interface Canvas {
    public void doDraw(a);
}

public class Test {
    public static void main(String[] args) {
        Canvas canvas = new Canvas(){
            public void doDraw(a) {
                System.out.println("draw any"); }}; canvas.doDraw();// output result draw any}}Copy the code
Parameterized anonymous inner class
public interface Draw {
    public void doThing(a);
}

public class Canvas {
    public void doDraw(Draw draw){ draw.doThing(); }}public class Test {
    public static void main(String[] args) {
        Canvas canvas = new Canvas();
        canvas.doDraw(new Draw() {
            @Override
            public void doThing(a) {
                System.out.println("draw a Circle"); }}); }}Copy the code

Why put this parameter anonymous inner class at the end, sharp-eyed friends will find it very similar to the original Swing code given at the beginning. While you may be confused by the first two, the last case column is already clear to anyone with a solid knowledge of technology. Spring’s JdbcTemplate has something similar in it. So much for anonymous inner classes, to summarize:

  • Anonymous inner classes do not have access modifiers.
  • Anonymous inner classes cannot have constructors.
  • Anonymous inner classes cannot define any static members, static methods.
  • An anonymous inner class can not be public, protected, private, static.
  • Only one instance of an anonymous inner class can be created.

conclusion

Talk about so many internal classes, nothing but let you have some understanding of the internal class, what unreasonable or wrong place, please excuse me. If you ask me do I use inner classes at all? That’s up to the developers. In fact, in most development processes, there are no special requirements that make it difficult to use inner classes. But I want to say that “parametric anonymous inner class”, we use a lot of development.