The difference between the local inner class and anonymous inner class in Java is explained in detail (attached source)

Source: Internet
Author: User

Objective
As mentioned earlier, you can create inner classes in a block of code, typically in a method body. A local inner class cannot have an access specifier because it is not part of a perimeter class, but he can access constants within the current code block, as well as all members of this perimeter class. The following example compares the creation of a local inner class to an anonymous inner class.
Sample source Code
Package com.mufeng.thetenthchapter;interface Counter {int next ();} public class Localinnerclass {private int count = 0; Counter Getcounter (final String name) {class Localcounter implements Counter {public localcounter () {//TODO auto-generate D Constructor stub//Local Inner class can have a constructorSystem.out.println ("Localcounter ()");} @Overridepublic int Next () {//TODO auto-generated method StubSystem.out.print (name); return count++;}} return new Localcounter ();} Counter GetCounter2 (final String name) {return new Counter () {//Anonymous inner class cannot have named Constructor,only an//instance Initializer{system.out.println ("Counter ()");} @Overridepublic int Next () {//TODO auto-generated method StubSystem.out.print (name); return count++;}};} public static void Main (string[] args) {Localinnerclass lic = new Localinnerclass (); Counter C1 = lic.getcounter ("Local inner"), C2 = Lic.getcounter2 ("Anonymous inner"); for (int i = 0; I < 5; i++) {System.out.println (C1.next ());} for (int i = 0; I < 5; i++) {System.out.println (C2.next ());}}}

Output results
Localcounter () Counter () Local inner 0Local inner 1Local inner 2Local inner 3Local inner 4Anonymous inner 5Anonymous inner 6 Anonymous Inner 7Anonymous inner 8Anonymous inner 9

SOURCE parsing
CounterReturns the next value in the sequence. We implemented this function using local inner classes and anonymous inner classes, respectively, with the same behaviors and capabilities. Since the name of the local inner class is not visible outside the method, why do we still use the local inner class instead of the anonymous inner class?       The only reason is that we need a named constructor, or we need to overload the constructor, and the anonymous inner class can only be used for instance initialization. So another reason to use local inner classes instead of anonymous inner classes is that more than one object of that inner class is required.

The difference between the local inner class and anonymous inner class in Java is explained in detail (attached source)

Related Article

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.