JAVA inner class details and examples

  • 2020-05-17 05:30:01
  • OfStack

JAVA inner class

1. What is an inner class?

The definition of one class is placed inside another class, which is called an inner class

2. What features do inner classes have?

1. The inner class is still a separate class. After compilation, the inner class will be compiled into a separate.class file, but it is preceded by the name of the outer class and the $symbol.
2. Inner classes cannot be accessed in a normal way. The inner class is a member of the outer class, so the inner class has free access to the member variables of the outer class, whether or not private.
3. If the inner class is declared as static, it cannot access the member variables of the outer class casually. At this time, the inner class can only access the static member variables of the outer class.

3. What are the inner classes?

1. Member inner class

Such as:


package com.test01;

public class A {
  //  The inner class  B  inheritance  TestPojo  And the implementation  TestInterface  interface 
  class B extends TestPojo implements TestInterface{
    // The inner class  B  Own way 
    public void run(){
      System.out.println(" I'm running! ");
    }
    // Overridden interface methods 
    public void testf() {
      System.out.println(" Implement the interface! ");
    }
  } 
  //  The inner class is called inside the method 
  public void test(){
    B b = new B() ;
    b.testf() ;    //  Override interface methods instead 
    b.run() ;     //  Call your own method 
    b.testpojo() ;   //  Calls methods that inherit from the parent class 
  }
  // main  Methods test 
  public static void main(String[] args) {
    A a = new A() ;
    a.test() ;
  }
}
//  Defines the 1 An interface   , the method for testf() 
interface TestInterface{
  public void testf() ;
}
//  Defines the 1 An ordinary class   methods testpojo() 
class TestPojo{
  public void testpojo(){
    System.out.println(" I'm simple pojo class ");
  }
}
//  implementation   Calls a method in an inner class 
class Textone{
  public static void main(String[] args) {
    A.B b = new A().new B() ;  // Call the class A Inside the inner class B
    /**  Is equivalent to   The following code 
     *     A a = new A() ;
     *     A.B b = a.new B() ;
     * */
    b.testf() ;   //  Override interface methods instead 
    b.run() ;    //  Call your own method 
    b.testpojo() ;  //  Calls methods that inherit from the parent class 
  }
}

2. Method inner class


package com.test01;

public class PerTest {
  public void test(){     //  define 1 A method of 
    class Ne{        //  define 1 Method inner class 
      public void fle(){  //  A method that defines a method inner class  
        System.out.println(" I'm flying! ");
      }
    } ;
    new Ne().fle() ;    // Calls the method of the inner class 
  }
  public static void main(String[] args) {
    new PerTest().test() ;  // test 
  }
}

Note :(1) method inner class can only be instantiated within the method that defines the inner class, and can not be instantiated outside the method.

(2) the inner class object of the method cannot use the non-final local variable of the method of the inner class.

Because a method's local variables are on the stack, they only exist for the lifetime of the method. When a method ends, its stack structure is deleted,
Local variables become history. But after the method ends, the inner class object created inside the method may still exist in the heap!
For example, if a reference to it is passed to some other code and stored in a member variable. Because there is no guarantee of local change
The amount of survivability and the length of a method inner class object are as long as 1, so inner class objects cannot use them. (this understanding comes from baidu baike)

3. Anonymous inner class

1) abstract anonymous inner class


package com.anonymous;

public class AbstractClass {
  public void test(){        // Methods for test
    TestA a = new TestA(){    // Implement abstract class 
      @Override
      public void run() {    // A method that implements an abstract class 
        System.out.println(" I'm using abstract anonymous inner classes ");
      }
    } ;
    a.run() ;          // Calls the method of the inner class 
  }
  public static void main(String[] args) {
    new AbstractClass().test() ;  // test 
  }
}
// define 1 An abstract class  TestA  The abstract method is run()
abstract class TestA{
  public abstract void run() ;
}

2) interface anonymous inner class


package com.anonymous;

public class TestAnonymous {
  MyInterface m = new MyInterface(){  // Implementing an interface 
    public void eat() {        // rewrite MyInterface  Interface methods 
      System.out.println(" I'm eating! ");
    }
  } ;
  public void ss(){  // methods ss
    m.eat() ;    // Call the overridden method 
  }
  public static void main(String[] args) {
    new TestAnonymous().ss() ;  // test 
  }
}
// define 1 An interface   Methods for  eat
interface MyInterface{
  public void eat() ;
}

Note: anonymous inner classes can be defined within a method or within a member of a class, and neither can be called directly by an external class

4. What are inner classes for?

Each inner class can inherit independently from an implementation, so whether or not the outer class has inherited an implementation does not matter to the inner class. Some design and programming problems are difficult to solve without the ability provided by inner classes to inherit from multiple concrete or abstract classes. From this perspective, inner classes complete the solution of multiple inheritance.

Interfaces solve part of the problem, while inner classes effectively implement "multiple inheritance."

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


Related articles: