Anonymous inner class __java in Java8

Source: Internet
Author: User
Tags class definition
First of all, review the definition and rules of anonymous internal classes, anonymous inner class is not the name of the inner class, the inner class that can put a class definition within the definition of another class, the use of internal classes can make the code more clear;
Note: Anonymous inner classes are suitable for creating classes that only need to be used once, and creating an anonymous inner class immediately creates an instance of the class, and the definition of the class disappears immediately;
Another 1. Anonymous inner class cannot be an abstract class, because an abstract class cannot create an object; 2. An anonymous inner class must inherit a parent class or implement an interface, and only one; 3. An anonymous inner class cannot define a constructor, which is easy to understand because the constructor must have the same name as the class name.
As shown in the following procedure, the anonymous inner class is created by inheriting the parent class, which implements the interface and resembles it;
Abstract class Animal {public
	abstract void Voice ();
}

public class Test {public
	static void Main (string[] args) {
		Animal a = new Animal () {public
			void voice () {
  SYSTEM.OUT.PRINTLN ("Woof!");
			}
		;
		A.voice ();
	}

Interface Animal {public
	void voice ();
}

The following are programs that do not use anonymous inner classes:

Abstract class Animal {public
	abstract void Voice ();
}

Class Dog extends Animal {public
	void voice () {
		System.out.println ("woof). ");
	}
}

public class Test {public
	static void Main (string[] args) {
		Animal a = new Dog ();
		A.voice ();
	}


Comparing the above two methods, we know that using anonymous inner class makes the program more concise;


Next, introduce the JAVA8 about the anonymous inner class;
Java8 the restriction that local variables accessed by local internal classes and anonymous inner classes must use the final modifier. If a local variable is accessed by an anonymous inner class, it is equivalent to automatically using final, but it is simple, but still be aware that there may be errors, such as the following example:

Interface Animal {public
	void voice ();
}

public class Test {public
	static void Main (string[] args) {
		int voicenumber = 2;//Sound size
		Animal a = new Animal () {Public
			void voice () {
				System.out.println (voicenumber);
			}
		};
		A.voice ();
	}



System.out.println (Voicenumber) before Java8; this line of code is prompted with an error that reads "cannot refer to a non-final variable the Voicenumber Inner class defined in a different method "must be decorated with final if you want to use Voicenumber, but for Java8, if you are in int voicenumber=2; followed by code voicenumber=3, the result is a compilation error, suggesting that the local variable referenced from the internal class must be the final variable or the actual final variable, because the local variable voicenumber is already accessed by the anonymous inner class, which is equivalent to the final modification;

Note:When writing with other tools such as MyEclipse, you may be prompted with an error without final, and I have an error when I use MyEclipse, but there is no error when using the command line test.
As for why use final, refer to http://android.blog.51cto.com/268543/384844
Finally, Java8 This function means that local variables that are accessed by anonymous internal classes can be either final or not, but must be used in a final-decorated way, that is, once assigned, the value cannot be assigned later.


The above section refers to the "crazy Java Handout", and added some of their own understanding, hoping to help.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.