Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

1. Member inner classes

1. The sample

InnerClass {public String SayHi() {return "Hello "; }}}Copy the code

Characteristics of 2.

  1. The inner class can access the member variables of the outer class unconditionally. To access the member variables of the inner class, the outer class needs to use new.
  2. An inner class has a variable or method with the same name as an outer class. Access the outer class by: outer class.this. Methods.
  3. An inner class depends on an outer class. An inner class has to be an outer class to have an inner class. To call the inner class method:
OutClass out = new OutClass();
OutClass.InnerClass inner = out.new InnerClass();
Copy the code
  1. Inner class permissions can be public,protected, protected type, or private. External classes can only be public and protected.
  2. Member inner classes cannot have the static keyword. Non-static inner classes can also define static members with final keywords.

2. Local inner classes

1. The sample

public class Outer { private int s = 100; private int out_i = 1; public void f(final int k) { final int s = 200; int i = 1; final int j = 10; Class Inner {int s = 300; // static int m = 20; Inner(int k) {inner_f(k); } int inner_i = 100; Void inner_f(int k) {// If the inner class does not have a variable with the same name as the outer class, the inner class can directly access the outer class instance variable system.out.println (out_i); // You can access the local variables of the external class (that is, the variables inside the method), but the variables must be final system.out.println (j); // System.out.println(i); Println (s) = system.out.println (s) = system.out.println (s); System.out.println(this.s); System.out.println(Outer.this.s); } } new Inner(k); } public static void main(String[] args) {Outer out = new Outer(); out.f(3); }}Copy the code

Characteristics of 2.

An inner class is defined in a class’s method, code block.

  1. Local inner classes can only be used in code blocks, method bodies, and scopes (such as creating objects and using class objects)
  2. Local inner classes access local variables in scope that require final modification.
  3. You can declare an abstract class using the abstract modifier.
  4. Can only be instantiated in the method that defines the class.

Static inner classes

1. The sample

public class Outer { private static int i = 1; private int j = 10; Public static void outer_f2() {} public void outer_f2() {} Public void outer_f2() {} Public void outer_f2() {  private static class Inner { static int inner_i = 100; int inner_j = 200; Println ("Outer. I "+ I); static void inner_f1() {// Static inner classes can only access static members of the Outer class (including static variables and static methods) system.out.println ("Outer. outer_f1(); // system.out.println ("Outer. I "+j); // system.out.println ("Outer. // outer_f2(); }} public void outer_f3() {// The outer class accesses the static member of the inner class: the inner class. Static member System.out.println(inner.inner_i); Inner.inner_f1(); Inner Inner = new Inner(); inner.inner_f2(); } public static void main(String[] args) { new Outer().outer_f3(); }}Copy the code

Characteristics of 2.

Inner class with static modifier.

  1. You cannot use non-static variables and methods of an external class without relying on it.
  2. You do not need to generate an outer class object to generate an inner class, as member inner classes do.
  3. Static inner classes can define static or non-static members.
  4. The outer class accesses a static member of the inner class: the inner class. Static member.
  5. An external class accesses a non-static member of an inner class: instantiate the inner class.
  6. You can have non-static methods or variables.

3. Application scenarios

  1. The inner class does not depend on the resources of the outer class when the outer class needs to use it.
  2. Ability to generate objects individually.
  3. Save resources.

4. Anonymous inner classes

1. The sample

It’s an inner class without a name. The format of the anonymous inner class:

New interface or superclass (){override abstract method};Copy the code

2. Application scenarios

You do not implement an interface, but want to override methods in the interface and call and return. Objective:

  1. You can make the mission name concise.
  2. Make the code more compact, concise, encapsulation is better than the inner class.
  3. A class can be used to extend another class or implement an interface without adding any other methods, just to override the inherited methods.
public class OuterClass { public InnerClass getInnerClass(final int num,String str2){ return new InnerClass(){ int number = num + 3; public int getNumber(){ return number; }}; Public static void main(String[] args) {OuterClass out = new OuterClass(); InnerClass inner = out.getInnerClass(2, "chenssy"); System.out.println(inner.getNumber()); } } interface InnerClass { int getNumber(); } ---------------- Output: 5Copy the code

If anonymous inner classes are not applicable

interface InnerClass { int getNumber(int a); } class InnerClassImpl{ int getNumber(int a){ system.out.print(a); } } InnerClass InnerClass = new InnerClassImpl(); InnerClass = new InnerClass (){public int getNumber(int a){return a; }}Copy the code

Features:

  1. Anonymous inner classes do not have access modifiers.
  2. New anonymous inner class, which should exist in the first place. If we comment out the InnerClass interface, we get a compilation error.
  3. Notice the getInnerClass() method parameter. The first parameter is final, but the second parameter is not. We also find that the second parameter is not used in the anonymous inner class, so when the method parameter needs to be used by the anonymous inner class, the parameter must be final.
  4. An anonymous inner class is the only class that does not have a constructor.
  5. Anonymous inner classes cannot define any static members, methods, or classes.

5. Reasons for using inner classes:

  1. Multiple inheritance can be implemented. (Different inner classes can inherit from different classes).
  2. Inner classes are better hidden.
  3. We can use anonymous inner classes when we don’t want to write implementations of interfaces or just use objects once.
  4. Each inner class is an individual.