SoFunction
Updated on 2025-03-08

Preliminary exploration of the use of internal Java classes

The concept of inner classes was not very clear before, so here I summarize the relationship between inner classes and outer classes and the way they are called.

Java internal classes can generally be divided into the following three types:

  • Member internal class
  • Static inner class
  • Anonymous internal class

1. Internal class of members

Creation of internal classes:The creation of internal class of member needs to rely on external class objects. It is impossible to create internal class objects before creating external class instances.

If you create a member internal class directly in the main function, an error will be reported:

MemberInner memberInner = new MemberInner();

Private property access:Member internal classes can directly access the non-static private properties of the external class. When external classes access the private properties of the internal class, they need to access it through the objects of the internal class.

In the memberInnerTest() method, you can directly access the private test field in the external class. If there is a field with the same name as the external class in the internal class, you need to use the following method to call the external class:

Create an inner class:

Create an inner class object inside an outer class: create an inner class object using the new keyword

private void createMemberInner(){
    MemberInner memberInner = new MemberInner();
    ();
}

Create an inner class object outside an outer class: you need to create an outer class object first, and then create an inner class through an outer class

OuterTest outerTest = new OuterTest();
MemberInner memberInner =  MemberInner();

If you do not need to use external classes, you can create an internal class using the following method

MemberInner memberInner = new OuterTest().new MemberInner();

Use this method to create an internal class to note: if there are multiple internal classes in the external class, this method will new multiple external class objects, and each class object is independent, so the member variables of the external class will not be shared in the internal class.

In the following code, since new two OuterVar classes are released, the member variable var output by InnerOne and InnerTwo classes are both 0. If you want InnerOne to share the member variable var with the InnerTwo class, you need to create it in the following way:

  OuterVar outerVar = new OuterVar();
  InnerOne innerOne =  InnerOne();
  InnerTwo innerTwo =  InnerTwo();
public class OuterVar {
private int var = 0;
 
class InnerOne{
  private void innerOnePrint(){
      ("innerOnePrint:"+var);
      var+=1;
  }
}
 
class InnerTwo{
  private void innerTwoPrint(){
      ("innerTwoPrint:"+var);
  }
}
 
public static void main(String[] args) {
  InnerOne innerOne = new OuterVar().new InnerOne();
  InnerTwo innerTwo = new OuterVar().new InnerTwo();
 
  ();
  ();
}
}
/* result
 innerOnePrint:0
 innerTwoPrint:0
 */
/*1. Member internal class
   2. Static internal class
   3. Anonymous internal class
 */
public class OuterTest {
    /*
     1. Internal class of members
      */
    private String test = "OuterTest";
 
    private void createMemberInner(){
        MemberInner memberInner = new MemberInner();
        ();
    }
 
    class MemberInner{
        private String test = "MemberInnerTest";
 
        public void memberInnerTest(){
            ("MemberInner");
            (test);
            ();
 
        }
    }
 
    public static void main(String[] args) {
        // Member internal class        OuterTest outerTest = new OuterTest();
        MemberInner memberInner =  MemberInner();
//        MemberInner memberInner = new OuterTest().new MemberInner();
        
        ();
    }
}
 
 
/* result
    MemberInner
    MemberInnerTest
    OuterTest */

2. Static internal classes

The static inner class is defined inside the external class and is modified with the static keyword. The static inner class does not need to be generated by external class objects, and cannot access the member domain of the external class, but can access the static domain.

Private property access:Static inner classes cannot directly access non-static properties of external classes

Create an inner class:

Create an inner class object inside an outer class: create an inner class object using the new keyword

private void createStaticInner(){
    StaticInner staticInner = new StaticInner();
    ();
}

Create an inner class object outside an outer class: you can create an inner class object directly without creating an outer class object

StaticInner staticInner = new StaticInner();
public class OuterTest {
    /*
       2. Static internal class
      */
    private String test = "OuterTest";
 
    private void createStaticInner(){
        StaticInner staticInner = new StaticInner();
        ();
    }
 
 
    static class StaticInner{
        private String test = "StaticInnerTest";
 
        public void staticInnerTest(){
            ("StaticInner");
            (test);
        }
    }
 
    public static void main(String[] args) {
        // Static inner class        StaticInner staticInner = new StaticInner();
        ();
    }
}
 
/* result
    StaticInner
    StaticInnerTest */

3. Anonymous internal class

Prerequisites for using anonymous inner classes: you must inherit a parent class or implement an interface. It has the following characteristics:

Since anonymous inner class does not have a class name, anonymous inner class cannot have a constructor

Anonymous inner classes cannot define any static members, methods, and classes

//Anonymous internal classinterface Father{
    public abstract void talk();
 
    public abstract void eat();
}
public class Test {
    public static void main(String[] args) {
        //Anonymous internal class        Father father = new Father() {
            @Override
            public void talk() {
                ("I'm Father");
            }
 
            @Override
            public void eat() {
                ("I'm eating");
            }
        };
        ();
        ();
    }
}
 
/* result
    I'm Father
    I'm eating */

This is the end of this article about the use of Java internal classes. For more related Java internal classes, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!