Static inner classes and Lambda expressions

1. Static internal classes
(1) Named static internal classes

static class Heart{

		{
			System.out.println("doing....");
		}
	}

(2) Anonymous static internal classes

class Father {

	public void eat() {
		System.out.println("Eat with chopsticks....");
	}
}

/**
 * External classes
 */
public class OutClass {

	/**
	 * Anonymous Inner Class
	 */
	static Father son = new Father(){
		
		@Override
		public void eat() {
			System.out.println("Eat with chopsticks....");
		}
	};
}

2. Usage:
(1) To create objects of non-static internal classes in non-static methods, we first create objects of external classes, which is implicit, and use this.

    public class Body {
    
    	class Heart{
    
    		{
    			System.out.println("doing....");
    		}
    	}
	
	public void get2(){
		
		Heart heart = new Body().new Heart();    //This method can also be used.
		
		Heart heart1 =this. new Heart();   //this can be omitted
		
		heart1 = this.new Heart();   //Think of class heart as a global variable and use global variable + this in the statement
	}
}

(2) To create objects of non-static internal classes in static methods, external class objects must be "displayed".

public class Body {

	class Heart{

		{
			System.out.println("doing....");
		}
	}
	
		public static void get1(){
		
		Heart heart2 = new Body().new Heart();   //this cannot be used in static methods, so you can only create objects, first external and then internal.
	}
}

(3) Create objects of static internal classes in non-static methods and directly create objects of classes.
(4) Create objects of static internal classes in static methods and directly create objects of classes.
Reason: Static methods are handled when classes are loaded. Static methods assign addresses directly and can be called directly. But it's not static. You have to create objects.
Be careful:
Think: Static member variables can be used "directly" in non-static methods or code blocks. Why?
Static processing in class loading: static variable direct assignment static method directly assigns address static code block to execute; non-static can only execute operations when creating objects: variable assignment method assigns address code block to execute.
Summary: The static operation of JVM is ahead of the non-static operation.
3.
(1) If the inner class is static, it can only be defined directly in the outer class. There can be no more methods in the method, only methods can be invoked in the method.
(2) Static members (static attributes, static code blocks, and static methods) are allowed only in named static inner classes.



4.Lambda expression
If the method has no return value and only one line of code, the Lambda expression grammar can be in this form: ([parameter 1], [parameter 2], [parameter 3],... [parameter n] - > single line statement, as follows:

@FunctionalInterface
interface IMammal {
	void move(String name);
}

public class Test {

	public static void main(String[] args) {
		IMammal whale = (name) -> {
			System.out.println(name+"Moving......");
		};
		whale.move("Whale");
	}
}

Lambda Expression:
@FunctionalInterface
interface IMammal {
	void move(String name);
}

public class Test {

	public static void main(String[] args) {
		IMammal whale = (name) -> System.out.println(name+"Moving......");
		whale.move("Whale");
	}
}

(2) If the method has a return value and only one line of code, the Lambda expression grammar can be in this form: ([parameter 1], [parameter 2], [parameter 3],... [parameter n] - > expression, as follows:

@FunctionalInterface
interface IComputer {
	int add(int a, int b);
}

public class Test {

	public static void main(String[] args) {
		IComputer computer = (a, b) -> {
			return a+b;
		};
		int result = computer.add(1,2);
		System.out.println(result);
	}
}

Lambda Expression:
@FunctionalInterface
interface IComputer {
	int add(int a, int b);
}

public class Test {

	public static void main(String[] args) {
		IComputer computer = (a, b) -> a+b;
		int result = computer.add(1,2);
		System.out.println(result);
	}
}

Keywords: Lambda jvm

Added by DoD on Tue, 06 Aug 2019 13:46:23 +0300