Java Learning Summary - Internal Classes

Internal class

Internal classes are classes defined within a class. For other classes in the same package, internal classes can be hidden from them.

  • Internal classes have access to all properties and methods of their external classes and do not need to create objects of external classes.
  • Objects of internal classes must be created, otherwise variables and methods of internal classes cannot be accessed through external classes.
  • If there are variables or methods with the same name as the external class in the internal class, then the variables and methods of the internal column will get higher priority than the variables and methods of the external class.
  • static variable cannot be defined

When do I use internal classes?
—— When a class is only called by this class, and other classes do not use it, it can be defined as an internal class, so that implementation details can be hidden and wrong calls can be avoided.

Define internal classes:

public class Outer{
    private int varOuter=100;
    class Inner    {
        int varInner=200;
        public void showOuter() {
            //Can it be output?
            System.out.println(varOuter); 
        }
    }
    public void showInner() {
        Inner i=new Inner();
        System.out.println(i.varInner);
    }
}

Access internal classes:

public class MyTest{
  public static void main(String[] args){
     //Instantiate external classes
     Outer o = new Outer();
     //Instantiate internal classes 
     Outer.Inner oi = o.new Inner(); 
   }
}

Static inner class

Internal classes identified by static are static internal classes.
Characteristic

  • Static internal classes, as static members of external classes, cannot access fee static members of external classes.
  • Amorphous inner classes can only define non-static members, while static inner classes can define static members and non-static members.
  • External classes can access static internal classes without using new

Define static internal classes:

public static class MyInner {
		public static void fun() {
			System.out.println("Here is the method of the inner class.");
		}
	}
public class MyOut {
    private int age;
    public  void show();
    static class  MyInner{
       private int name;
    }
}

Instantiate static internal classes (non-static internal classes cannot be instantiated using this method):

Outer.Inner inn=new Outer.Inner();

Direct access to static internal classes:

Outer.MyInner.fun();

Local inner class

Internal classes defined in a method body of a class or within a program block.
Characteristic:

  • Local variables of final type in methods can only be accessed in internal classes defined by methods
    For example:
public class Outer2 {
      public int a = 1;
      private int b = 2;
      public void method( final int c) {
          int d = 3;
          final int e = 2;
          class Inner {
              private void iMethod( int e) {
                  //System.out.println(e);
              }
          }
      }
  }

Internal classes in classes:

class  A{
    int a;
    public void method(){     
    }
    class B{ }
}

Local internal classes:

class A{
    int a;
    public void method( int c ){
        int b=0;
        class B{ }
    }
}

Anonymous Inner Class

An inner class without a class name is an anonymous inner class.
Characteristic

  • Anonymous inner class has no access modifier
  • new Anonymous Internal Class. This external class first needs to have a parent class or interface
  • External class method parameters or local variables need to be used by anonymous internal classes, must be final, and can only be used without modifying assignments

Call an anonymous inner class:

public abstract class Bird {  
  public abstract int fly();  
} 
public class Test {  
    public static void main(String[] args) {
        final int begin = 1000;  
        Bird swift = new Bird() {                
            public int fly() {  
                return 10000 + begin;  
            }  
         } 
    }
} 

Before Java 8, if you want to pass behavior into a function, the only choice is to use anonymous inner classes. In later versions of JDK8, lambda expression is introduced. Lambda expression is used as an anonymous method to pass behavior into a function.
Syntax: (parameter) - > {method body}

Example:

public interface LambdaInterface {
  void show(String str);
}

Write in internal class:

class A implements LambdaInterface {
			@Override
			public void show(String str) {
				 System.out.println(str);
			}
		}
		LambdaInterface lambda =new A();
		lambda .show("Hello");

The Lambda expression is () - > {}, and the contents of {} are the concrete implementation of abstract methods and methods in implements'interfaces.
Write in lambda expression:

public class LambdaTest {
  public static void main(String[] args) {
    LambdaInterface lambda = (s) -> {
      System.out.println(s);
    };
    lambda.show("Hello");
  }
}

The Difference between Lambda Expressions and Anonymous Internal Classes

  • Anonymous inner classes can create instances for any interface -- no matter how many abstract methods there are, as long as anonymous inner classes implement all methods; but Lambda expressions can only create instances for functional interfaces
  • Anonymous inner classes can create instances for abstract classes or even ordinary classes; however, lambda expressions can only create instances for functional interfaces.
  • The abstract method body implemented by anonymous inner classes allows the invocation of default methods in interfaces; however, the code block of Lambda expressions does not allow the invocation of default methods in interfaces.

Similarities between Lambda expressions and anonymous inner classes

  • Lambda expressions, like anonymous internal classes, have direct access to local variables of "effective final" and member variables of external classes (including strength variables and class variables).
  • Like objects created by anonymous inner classes, Lambda expressions can directly invoke default methods inherited from interfaces.

Keywords: Lambda Swift Java

Added by shae marks on Tue, 20 Aug 2019 13:35:48 +0300