Details and examples of java inner classes (anonymous classes anonymous objects static inner classes)

  • 2020-05-26 08:33:53
  • OfStack

An introduction to inner classes

A class defined in another class is called an inner class

Member inner class

1.. To create a member inner class, new must first create an instance of the outer class, and then create an object of the inner class through.new

2.. this can access all the properties and methods of the external class through the class name of the external class.this can access all the properties and methods of the external class.


public class Test1 {

  String name = "asnd";

  public static void main(String[] args) {

    Test1 test1 = new Test1();
    Inner mInner = test1.new Inner();
    mInner.print();
  }

  void show() {
    System.out.println("show");
  }

  public class Inner {

    String name = "123";
    private void print(){
      show();
      System.out.println(name);// Print is 123
      System.out.println(Test1.this.name);// Print is asnd
    }
  }
}

Anonymous inner class

A class without a name creates an object at the same time as the class is created.

You can use anonymous inner classes if you only need to use them once


  File file = new File("D:/cc.txt") {

      @Override
      public boolean delete() {
        System.out.println(" Whether or not to delete y/n");
        Scanner input = new Scanner(System.in);
        String str = input.next();
        if (str.equals("y")) {
          return super.delete();
        }
        System.out.println(" Delete failed ");
        return false;
      }
    };
    file.delete();

  }

Anonymous objects

This object only needs to be accessed once.


new Thread() {

      @Override
      public void run() {
        System.out.println(" Thread starts !");
        try {
          Thread.sleep(2000);
          System.out.println(" End of the thread !");
        } catch (Exception e) {
          e.printStackTrace();
        }
        super.run();
      }

    }.start();

Static inner class

1. Static inner class can only access static methods and variables of external class, not non-static.

2. Static inner classes can be created without creating a reference to an external class.

Anonymous inner classes access local variables

Internal class access local variables must be final, if not, jdk1.8 added by default
The following method can be used when the variables used are changing, or the following i can be static at the beginning


for (int i = 0; i < 5; i++) {
      final int finali = i;
      new Thread() {
        public void run() {
          System.out.println(finali);
        };
      }.start();

    }

Here are some tips for implementing inner classes


public static void main(String[] args) {
    Lam mLam = new Lam();
    // The first 1 A method of implementation 
    mLam.to(new Ligh() {
      @Override
      public void shin() {
        System.out.println("on The first 1 methods ");
      }
    });
    // The first 2 Kind of implementation method 
    class MyLam implements Ligh{

      @Override
      public void shin() {
        System.out.println(" The first 2 Kind of ");

      }}

    mLam.to(new MyLam());
  }
}

interface Ligh {
  void shin();
}

class Lam {
  public void to(Ligh ligh) {
    ligh.shin();
    System.out.println("on");
  }
}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: