[Java basics] 009 internal classes

concept

In Java, a class can be defined in another class or a method. Such a class is called an inner class.

In a broad sense, internal classes generally include these four types: member internal classes, local internal classes, anonymous internal classes and static internal classes.

Member inner class

Member inner class is the most common inner class, which is defined as being inside another class

class Circle {
    double radius = 0;
     
    public Circle(double radius) {
        this.radius = radius;
    }
     
    class Draw {     //Inner class
        public void drawSahpe() {
            System.out.println("drawshape");
        }
    }
}

In this way, the class Draw looks like a member of the class Circle, which is called the external class. The inner class of a member can unconditionally access all member properties and member methods of the outer class (including private members and static members).

However, it should be noted that when the internal class of a member has a member variable or method with the same name as the external class, hiding will occur, that is, the members of the internal class of the member 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 method

In the external class, if you want to access the members of the internal class of the member, you must first create an object of the internal class of the member, and then access it through a reference to this object:

class Circle {
    private double radius = 0;
 
    public Circle(double radius) {
        this.radius = radius;
        getDrawInstance().drawSahpe();   //You must create an object of a member's inner class before accessing it
    }
     
    private Draw getDrawInstance() {
        return new Draw();
    }
     
    class Draw {     //Inner class
        public void drawSahpe() {
            System.out.println(radius);  //private member of external class
        }
    }
}

The internal class of a member is dependent on the external class, that is, if you want to create an object of the internal class of a member, the premise is that there must be an object of the external class.

public class Test {
    public static void main(String[] args)  {
        //The first way:
        Outter outter = new Outter();
        Outter.Inner inner = outter.new Inner();  //It must be created through the outer object
         
        //The second way:
        Outter.Inner inner1 = outter.getInnerInstance();
    }
}
 
class Outter {
    private Inner inner = null;
    public Outter() {
         
    }
     
    public Inner getInnerInstance() {
        if(inner == null)
            inner = new Inner();
        return inner;
    }
      
    class Inner {
        public Inner() {
             
        }
    }
}

↑ ↑ ↑ important

Local inner class

The difference is that it is limited to a method within the scope of the class or within the scope of the class.

class People{
    public People() {
    }
}
 
class Man{
    public Man(){
    }
     
    public People getWoman(){
        class Woman extends People{   //Local inner class
            int age =0;
        }
        return new Woman();
    }
}

A local internal class is like a local variable in a method. It cannot have public, protected, private and static modifiers.

Anonymous Inner Class

Anonymous inner classes should be the most commonly used when we write code. Using anonymous inner classes when writing event listening code is not only convenient, but also makes the code easier to maintain.

This code sets the listener for the two buttons, which uses the anonymous inner class.

new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         
    }
}

Anonymous inner classes are the only classes without constructors. Because it has no constructor, the scope of use of anonymous inner classes is very limited. Most anonymous inner classes are used for interface callback. The anonymous inner class is automatically named outer $1 by the system when compiling class.

Static inner class

Static internal class is also a class defined in another class, but there is a keyword static in front of the class. The static inner class does not need to depend on the outer class, which is similar to the static member property of the class, and it cannot use the non static member variables or methods of the outer class.

Keywords: Java Back-end

Added by joeysarsenal on Fri, 04 Mar 2022 23:42:35 +0200