A detailed summary of inner classes in Java

  • 2020-04-01 02:23:30
  • OfStack

Inner classes are not easy to understand, but they are simply a class that contains another class

Just as a person is made up of physical results such as brain, limbs, and organs, the inner class corresponds to one of these organs, such as the heart: it also has its own properties and behaviors (blood, beats).

Obviously, you can't represent a heart unilaterally with attributes or methods here, you need a class

And the heart is in the body, just as the inner class is in the outer

Example 1: the basic structure of an inner class


//Outside class
class Out { 
    private int age = 12; 

    //The inner class
    class In { 
        public void print() { 
            System.out.println(age); 
        } 
    } 
} 

public class Demo { 
    public static void main(String[] args) { 
        Out.In in = new Out().new In(); 
        in.print(); 
        //Or access it the following way
        
    } 
}

Results: 12

As you can see from the above example, inner classes are a terrible way to break good code structure, but why use inner classes at all?

Because inner classes are free to use the member variables of outer classes (including private ones) without having to generate objects of outer classes, this is the only advantage of inner classes

Like the heart, it can access the body's blood directly, rather than having it drawn by a doctor

The program compiles to produce two.class files, out.class and Out$in.class

The $represents the one In out.in In the program above.

Out.In = new Out(). New In() can be used to generate objects for inner classes, and there are two small things to note about this method

1. The Out at the beginning is to indicate which external class the inner class object to be generated is in

2. You must have an object of an external class before you can generate an object of an internal class, because the purpose of an internal class is to access member variables in an external class

Example 2: variable access form in inner class


class Out { 
    private int age = 12; 

    class In { 
        private int age = 13; 
        public void print() { 
            int age = 14; 
            System.out.println(" Local variables: " + age); 
            System.out.println(" Inner class variables: " + this.age); 
            System.out.println(" External class variables: " + Out.this.age); 
        } 
    } 
} 

public class Demo { 
    public static void main(String[] args) { 
        Out.In in = new Out().new In(); 
        in.print(); 
    } 
} 

Operation results:

Local variable: 14
Inner class variable: 13
External class variable: 12

As you can see from example 1, when an inner class has no member variables and local variables of the same name, the inner class accesses the member variables of the outer class directly without specifying the out.this.attribute name

Otherwise, local variables in the inner class override member variables in the outer class

The name of this. Property can be used to access the member variables of the inner class itself, and the name of this. Property can be used to access the member variables of the outer class

Example 3: static inner class


 class Out { 
    private static int age = 12; 

    static class In { 
        public void print() { 
            System.out.println(age); 
        } 
    } 
} 

public class Demo { 
    public static void main(String[] args) { 
        Out.In in = new Out.In(); 
        in.print(); 
    } 
} 

Results: 12

As you can see, if the internal static is static with static, then the internal class can only access the static member variables of the external class, which has limitations

Secondly, since the inner class is static, out.in can be viewed as a whole, and the object of the inner class can be directly new (it doesn't matter whether the outer class object is generated or not if it is accessed through the class name).

Instance 4: private inner class


 class Out { 
    private int age = 12; 

    private class In { 
        public void print() { 
            System.out.println(age); 
        } 
    } 
    public void outPrint() { 
        new In().print(); 
    } 
} 

public class Demo { 
    public static void main(String[] args) { 
        //This method is invalid
        
        Out out = new Out(); 
        out.outPrint(); 
    } 
} 

Results: 12

If an inner class only wants to be manipulated by methods in an outer class, you can declare an inner class using private

In the above code, we have to generate the object of the In class In the Out class to operate on, and we can no longer use out.in In = new Out().new In() to generate the object of the inner class

That is, the inner class is controlled only by the outer class

Like, my heart is controlled only by my body, and no one else has direct access to it

Example 5: method inner class


 class Out { 
    private int age = 12; 

    public void Print(final int x) { 
        class In { 
            public void inPrint() { 
                System.out.println(x); 
                System.out.println(age); 
            } 
        } 
        new In().inPrint(); 
    } 
} 

public class Demo { 
    public static void main(String[] args) { 
        Out out = new Out(); 
        out.Print(3); 
    } 
} 

Operation results:
3
12

In the above code, we moved the inner class to the methods of the outer class, and then regenerated an inner class object from the methods of the outer class to call the inner class methods

If at this point we need to pass arguments into the methods of the external class, then the method parameters of the external class must use the final definition

As for final, there is no special meaning here, just a form of expression


Related articles: