编程人 cdmana.com

New features of jdk8

JDK8 New features

  • Methods with method bodies can be defined in interfaces , If it's not static , Must use default modification ; If it's static, it's not necessary .

      class Test {
      	public void run() {
      		final int x = 10;
      		class Inner {
      			public void method() {
      				System.out.println(x);
      			}
      		}
      
      		Inner i = new Inner();
      		i.method();
      	}
      }
    

The local inner class must use... To access the local variables in its method final modification , Why? ?
Because when this method is called , Local variable if not used final modification , His life cycle is the same as the method's , When the way to play the stack , This local variable will also disappear , So if the local inner class object hasn't disappeared immediately, you want to use this local variable , There is no the . If you use final The modifier will enter the constant pool when the class is loaded , Even the way to play the stack , The constant of the constant pool is still , You can also continue to use .

eclipse Yes JDK8 I won't support it , So we should JDK Configuration to editplus in .

class Demo1 {
	public static void main(String[] args) {
		/*Demo d = new Demo();
		d.print();

		Inter.method();*/

		Demo d = new Demo();
		d.run();
	}
}

interface Inter {
	public default void print() {
		System.out.println("Hello World");
	}

	public static void method() {
		System.out.println("static method");
	}
}

class Demo implements Inter {
	public void run() {
		//JDK8, Ahead final It can be omitted 
		int num = 10;
		class Inner {
			public void fun() {
				System.out.println(num);
			}
		}

		Inner i = new Inner();
		i.fun();
	}
}
Scroll to Top