Understanding internal classes in java

Posted by fireice87 on Sat, 22 Jan 2022 18:03:10 +0100

1. Internal class definition

Inner class: define a class in another class or a method. Such a class is called inner class. An inner class is also an encapsulation (protection)
The internal class is similar to the real-life car and engine, human and heart.
be careful:

  • Internal classes and external classes can easily access each other's private domains.
  • The internal class can directly access the private properties of the external class, but if the external class wants to access the private properties of the internal class, it must create an internal class object and then access it through the created internal class object.
  • The inner class is a relatively independent individual, which is not related to the outer class (human body and heart)
// Internal classes and external classes can easily access each other's private domains, people and hearts
public class Outter {
    private String msg="outter Class mag attribute";
    // ===================================================
    // Internal class: all heart engines belong to private internal classes, which are completely hidden from the outside and only used inside the class
    class Inter{
        private int num=10;
        public void test(){
            // Direct access to msg private properties of external classes
            System.out.println(msg); // mag attribute in outer class
        }
    }
    //====================================
    // Define a method in an external class
    public void fun(){
        // Access the private properties of the inner class through the inner class object
        Inter inter = new Inter();
        // Access the private properties of the inter class
        System.out.println(inter.num); // 10
        inter.test();
    }
    public static void main(String[] args) {
        Outter outter=new Outter();
        outter.fun();
    }
}

Operation result: 10
mag attribute in outer class

Internal classes are generally divided into four types: member internal classes, static internal classes, method internal classes and anonymous internal classes

2. Member inner class

Member inner class: a class defined directly without any modifier (static) in the class
Member internal classes can be compared with member methods / properties. Member methods can access static fields and cannot own static fields.
be careful

  • The inner class of a member must depend on the outer class.
  • Can a member inner class define a static variable? No, the inner class of a member must depend on the outer class. If the inner class of a member has static properties, it can be accessed without an outer class.
    There are two ways to create class objects within members:
  • 1. Internal creation of external class
    It's no different from using other common classes

Internal class name reference = new internal class ();
Inter inter = new Inter();

  • 2. External class creation
    Create an internal class object outside the external class - the internal class should be visible to the outside (access problem)

External class name Internal class reference = new external class () New inner class ();
Outter.Inter in=new Outter().new Inter()

3. Static inner class (analogy to static method)

Static inner class: an inner class defined in a class and decorated with static
be careful

  • Difference from member inner class: static inner class does not need to rely on external class objects!
  • Static internal classes cannot use non static classes (including properties and methods) of any external class, but they can have their own member variables.
  • Static internal classes are ordinary classes, which are just nested inside the class
  • The internal class of a member can access the member domain and static domain of the external class, but cannot own the static domain; Static internal classes can have member domains, but they cannot directly access the member domains of external classes. Static domains can be accessed casually. Static methods can access static domains, but not member domains
    There are two ways to create static inner class objects
  • 1. Internal of external class
// test member method
    public void test(){
        Inner inner=new Inner();
    }
    // main static method
    public static void main(String[] args) {
        Inner inner=new Inner();

    }
  • 2. External of external class
    External class Internal class reference = new external class Internal class ();

4. Method inner class (analogy to static method)

Method internal class: a class defined directly inside a method. It is not allowed to use any access modifiers. It is completely hidden from the outside. There is no method outside.
be careful:

  • Method inner class cannot define static domain, except that it is the same as inner class of member
  • If the method parameter is used in the internal class of the method, the parameter is an implicit final declaration
// Method inner class
public class MthodClass {
    public void fun(int num){
        // The Inner method is an internal class and cannot have any modifiers
        class Inner{
            public  void test(){
                System.out.println(num); // 10
            }
        }
        // Inner does not use the num variable at this time
        //num++;
        //System.out.println(num);
        // When Inner uses the num variable, Num becomes implicit final, and the value cannot be modified
        System.out.println(num);
    }
    public static void main(String[] args) {
        MthodClass mthodClass=new MthodClass();
        mthodClass.fun(10);
    }
}

4. Anonymous inner class (predecessor of Lambda expression - functional programming)

Anonymous inner class is a special type of inner class of a method, and the class name is not written.
Anonymous inner classes comply with all requirements of method inner classes
Anonymous inner classes inherit a class or implement an interface by default.

Topics: Java Back-end