头图
This article participated in the Sifu technical essay , and you are welcome to join.

inner class

Basic overview


  • When the definition of a class is placed in the entity of another class, the class is called an inner class, and the class in which the class is located is called an outer class
  • What can appear in a class body: member variables, member methods, constructors, building blocks, static statement blocks, static variables, methods, inner classes
  • nested class

    • Inner classes (member inner classes, local inner classes, anonymous inner classes)
    • static nested class

syntax format

 class 外部类类名{
    class 内部类类名{
        内部类类体;
    }
}

member inner class


  • A member of an inner class is an inner class defined in another class or interface
  • Precautions

    • An outer class object must be created before a member inner class object can be created
    • Cannot contain static variables, static code blocks, static methods (except static constants)
    • Outer class can call inner class private member through member inner class object
    • A member inner class is a separate class, compiled into a separate .class file
  • effect

    • Member inner classes can access both outer class information and parent class information, thus making the solution of multiple inheritance complete
  • case

    • Outer.java

       // 成员内部类的使用
      public class Outer { // 外部类
      
      private String str = "Outer类的str1";
      private String str2 = "Outer类的str2";
      
      /*
       * 第一:内部类的名称不能与外部类重名
       * 第二:可以使用final、访问修饰符修饰
       */
      public class Inner{ // 内部类
      
          private String str = "Inner类的str1";
          private String str2 = "Inner类的str2";
          
          public void show() {
              // 内部类的成员优先于外部类成员
              System.out.println(str);
              System.out.println(str2);
              
              // 使用"外部类.this.成员"访问外部类的成员
              System.out.println(Outer.this.str2);
              System.out.println(Outer.this.str);
          }
          
      }
      
      }
    • TestOuter.java

       import Inner.demo1.Outer.Inner;
      
      public class TestOuter {
      
      public static void main(String[] args) {
          // 如果要创建内部类,那么在此之前需要先创建外部类对象
          
          // 创建外部类对象
          Outer o = new Outer();
      
          // 创建内部类
          Inner inner = o.new Inner();
          inner.show();
          
      }
      }

      str1 of class Inner
      str2 of class Inner
      str2 of Outer class
      str1 of Outer class

local inner class


  • A local inner class is an inner class defined in a method h or a code block
  • Precautions:

    • Cannot contain static variables, static code blocks, static methods
    • Can only be used in a method or code block where the class is defined, must be defined before use
    • When accessing the local variables of all its methods, the j local variable must be a valid constant
    • Is a separate class, compiled into a separate .class file
    • Only abstract and final modification can be used
    • When defining a static block or method, only the static members of the outer class can be accessed
  • case

    • Outer.java

       // 局部内部类
      public class Outer { // 外部类
      
      private String str1 = "Outer类中的str1";
      private String str2 = "Outer类中的str2";
      
      // 定义一个外部类的方法
      public void print() {
          System.out.println("Outer类中的print方法");
      }
      
      static {
          class Inner{}  // 局部内部类
      }
      
      public void method() {
          
          // 必须在使用之前定义 
          class Inner{
              private String str1 = "Inner类中的str1";
              
              // 内部类成员优先于外部类成员
              public void visitOuter() {
                  System.out.println(str1);
                  System.out.println(str2);
                  print();  // 直接跳到外部类方法
              }
          }
          
          // 局部内部类只能在定义它的方法或代码块中使用
          // 只能使用abstract/final修饰,不能与外部类重名
          Inner in = new Inner();
          in.visitOuter();
      }
      }
    • TestOuter.java

       public class TestOuter {
      
      public static void main(String[] args) {
          
          Outer o = new Outer();
          o.method();
      }
      }

      str1 in Inner class
      str2 in Outer class
      print method in Outer class

anonymous inner class


  • An anonymous inner class is an inner class without a name created when it is instantiated directly using an interface or a parent class
  • syntax format

    • interface/superclass type reference name = new interface/superclass type(){
      Override the method;
      }
  • Precautions

    • Must inherit only one parent class or implement one interface
    • No class keyword, no class name
    • is a special local inner class
    • Can only be used once
    • Can't define constructor
    • An anonymous class cannot be an abstract class
  • Advantages and functions

    • Anonymous inner classes can make code more compact and concise
    • Better packaging
    • Anonymous inner classes can be created flexibly
    • Anonymous inner classes make multiple inheritance solutions more complete
  • case

    • InterDemo.java

       public interface InterDemo {
      
      public abstract void print();
      }
    • Outer.java

       public class Outer { // 外部类
      
      private String str1 = "Outer类中str1";
      private String str2 = "Outer类中str2";
      
      // 匿名内部类
      InterDemo id = new InterDemo() {
          
          private String str1 = "匿名内部类str1";
          
          @Override
          public void print() {
          
              System.out.println(str1);
              System.out.println(str2);
              
              // 如果想要调用外部类的变量 ,  外部类.this.变量
              System.out.println(Outer.this.str1);
          }
      };
      
      public void show() {
          id.print();
      }
      }
    • TestOuter.java

       public class TestOuter {
      
      public static void main(String[] args) {
          
          Outer o = new Outer();
          o.show();
      }
      }

      anonymous inner class str1
      str2 in Outer class
      str1 in Outer class

static nested class


  • A static nested class is defined in another class, interface, nested class modified with the static keyword
  • Precautions

    • No need to generate outer class object to generate static nested class object
    • Only static members of the outer class can be accessed directly
    • The outer class can call the members of the inner class through the object of the static nested class
    • Can define static member variables or static member methods
  • case

    • Outer.java

       public class Outer { // 外部类 
      
      // 创建两个成员变量   一个静态,一个非静态
      private String str1 = "外部类的str1";
      private static String str2 = "外部类的str2";
      
      // 成员方法,静态
       public static void show1() {
          System.out.println("外部类的show方法");
      }
      
      // 静态嵌套类
      // 只能定义在类的成员位置,不能定义在方法或代码块中
      public static class Inner{
          
          private String str1 = "嵌套类的str1";
          // private static String str2 = "嵌套类的str2";
          
          public void show() {
              System.out.println(str2);
              System.out.println(str1);
              
              // 不能直接访问外部类的非静态成员
              // 但是可以直接访问外部类的静态成员
              show1();
          }
      
      }
      
      }
    • TestOuter.java

       import Inner.demo4.Outer.Inner;
      
      public class TestOuter {
      public static void main(String[] args) {
          
          // 如何创建内部类或者静态嵌套类对象
          // 首先创建外部类的对象
          Inner Inner = new Outer.Inner();
          Inner.show();
      }
      
      }

      str2 of outer class
      str1 of nested class
      show method of outer class

Difference between static nested class and non-static nested class

name Inner class (non-static nested class) static nested class
defined position Member locations, methods, code blocks Only in member position of outer class
composition Instance members, static constants, constructors Instance members, static members, static code blocks, constructors
object creation There must be an object of the outer class first Does not depend on external class instance, can be instantiated directly
access external classes Can directly access all members of the outer class Only static members of the outer class can be accessed directly

若尘
54 声望11 粉丝