A preliminary analysis of Java internal class knowledge

Posted by abhi on Fri, 11 Feb 2022 23:07:43 +0100

We all know that Java is composed of classes. There may be other classes in Java classes, which is the internal class in Java. However, there is more than one internal class in Java. There are four internal classes: ① member internal class, ② static internal class, ③ local internal class and ④ anonymous internal class. The functions of each internal class are different. We will explain the knowledge of each internal class in detail below.

Member inner class

Member inner class refers to a class that exists as a member of a class and is defined inside the class.

The implementation and operation of internal classes of members will be directly displayed in Code:

package com.tang.test;

//This is an external class
public class Outer {
    private int number = 10;

    //This is an inner class
    public class Inner{
        int number1 = 20;
        public void test(){
            System.out.println("Accessing the in an external class number: "+number);
            System.out.println("Access the in the inner class number1: "+number1);
        }
    }

    //This is a test class
    public static void main(String[] args) {
        Outer outer = new Outer();
        Inner inner = outer.new Inner();
        inner.test();
    }
}

Note how to create internal classes:

Inner inner = outer.new Inner();

When we create an internal class, we create it through the object of the external class. In addition, when there are member variables and methods with the same name between the external class and the internal class, the internal class can directly access the member variables and methods of the internal class. If we need to access the member variables and methods of the external class, we need to use the this keyword. The detailed code is as follows:

package com.tang.test;

//This is an external class
public class Outer {
    private int number = 10;

    //This is an inner class
    public class Inner{
        int number = 20;
        public void test(){
            System.out.println("Accessing the in an external class number: "+Outer.this.number);
            System.out.println("Access the in the inner class number: "+number);
        }
    }

    //This is a test class
    public static void main(String[] args) {
        Outer outer = new Outer();
        Inner inner = outer.new Inner();
        inner.test();
    }
}

Static inner class

Static internal class refers to the internal class modified by static. The characteristic of static internal class is that it cannot directly access the non static members of external class, but we can operate through new external class

package com.tang.test;

//This is an external class
public class StaticOuter {
    private int number = 12;

    //This is an inner class
    public static class Inner{
        int number1 = 24;
        public void show(){
            System.out.println("This is the data of calling the external class:" + new StaticOuter().number);
            System.out.println("This is the data of calling the internal class:" + number1);
        }
    }

    //This is the test method
    public static void main(String[] args) {
        StaticOuter staticOuter = new StaticOuter();
        Inner inner = new Inner();
        inner.show();
    }
}

Note that at this time, the variables in our external class are not static data, so we must create external objects to call.

If the variable of our external class is static at this time, we can call it directly. The detailed code is as follows:

package com.tang.test;

//This is an external class
public class StaticOuter {
    private static int number = 12;

    //This is an inner class
    public static class Inner{
        int number1 = 24;
        public void show(){
            System.out.println("This is the data of calling the external class:" + number);
            System.out.println("This is the data of calling the internal class:" + number1);
        }
    }

    //This is the test method
    public static void main(String[] args) {
        StaticOuter staticOuter = new StaticOuter();
        Inner inner = new Inner();
        inner.show();
    }
}

At this time, if the variable names of our external class and static internal class are different, we can call them directly as above. If they are the same, they are the same as the member internal class we directly talk about, and they can be called through the class name of the external class. The detailed code is as follows:

package com.tang.test;

//This is an external class
public class StaticOuter {
    private static int number = 12;

    //This is an inner class
    public static class Inner{
        int number = 24;
        public void show(){
            System.out.println("This is the data of calling the external class:" + StaticOuter.number);
            System.out.println("This is the data of calling the internal class:" + number);
        }
    }

    //This is the test method
    public static void main(String[] args) {
        StaticOuter staticOuter = new StaticOuter();
        Inner inner = new Inner();
        inner.show();
    }
}

The main point about static internal classes is that the way we create internal classes is different from that of member internal classes. We no longer need to create internal classes by creating external class objects. This is particularly important. It is an important distinction between member inner classes and static inner classes.

Local inner class

The internal method of the class is also called the internal method of the class, which is also an important part of the internal code of the class

package com.tang.test;

public class MethOuter {
    private int number = 19;
    final private int number2 = 29;

    public void show(){
        class Inner{
            int number3 = 52;
            public void print(){
                System.out.println("To access constants in an external class:" + number);
                System.out.println("To access variables in an external class:" + number2);
                System.out.println("To access variables in an internal class:" + number3);
            }
        }
        Inner inner = new Inner();
        inner.print();
    }

    public static void main(String[] args) {
        MethOuter methOuter = new MethOuter();
        methOuter.show();
    }
}

The local internal class is mainly the internal class of the method. We create the internal class in the method. In the test class, we can directly create an external class to call the test.

Anonymous Inner Class

Anonymous inner class is an inner class without a name. It can only be used once. Its main function is to simplify relevant codes, such as setting listeners and other related operations. Anonymous inner classes must inherit abstract classes or interfaces and cannot be generated out of thin air.

We will explain the knowledge points of anonymous inner classes in two ways

① : implement the anonymous inner class of the interface. The detailed code is as follows:

package com.tang.test;

public class Anonymous {
    public static void main(String[] args) {
        new animal(){

            public void eat(){
                System.out.println("Animals want to eat!");
            }
            @Override
            public void say() {
                System.out.println("What does an animal call mean");
            }
        }.eat();
    }
}

interface animal{
    public void say();
}

At this point, we can call the method. If we want to continue to call the anonymous inner class itself, we can start creating objects or calling them directly. say().

② : by implementing the anonymous inner class of the abstract class, the detailed code is as follows:

package com.tang.test;

public class Anonymous {
    public static void main(String[] args) {
        animal animal = new animal() {
            @Override
            public void say() {
                System.out.println("I want to eat");
            }
        };
        animal.say();
    }
}

abstract class animal{
    public abstract void say();
}

The above two methods are the expression of anonymous inner classes. We can reduce our corresponding code through anonymous inner classes, but we must also know enough about anonymous inner classes.

My blog is mainly a preliminary explanation of the internal class. There are still many detailed knowledge points that have not been written. If there are any incorrect ones, please point them out for correction.

Topics: Java p2p