Use of the Java inner class, anonymous inner class, nested class

Source: Internet
Author: User
Tags getbase

We all know that Java classes can be decorated by public, default (default, not write), meaning that they are allowed to be publicly accessible and that only other classes within the package are allowed access, whereas classes within the same package are often collaborating to accomplish the same functionality. In addition, we encounter classes that are only part of the implementation or composition of some other class, and they should not be created independently, and when they are created, they need to be linked to a "host", which is the inner class. Just like the heart of the human being, you can't come out alone, it can only depend on the human body to survive (this is what it exists for), at least modern medicine does not survive in vitro, what modern medicine can do is to take a heart out and switch it to another person immediately.
Inner classInner classes are typically part of the host class implementation or are a tool provided externally by the host. If the inner class is merely an implementation service for the host, the inner class can be decorated as private, which restricts access to the external classes.     As an inner class of tools, the general access modifier is public or default.     For ease of use, Java allows direct access to the members of the host class within the inner class (which also determines that you cannot directly new an inner class object outside the host class). The following is an example of a simple implementation of a container iterator using an inner class.
Interface Selector {Boolean end ();     Object current (); void next ();}     public class Sequence {private object[] items;     private int next = 0;     public Sequence (int size) {items = new object[size];          } public void Add (Object x) {if (Next < Items.length) {items[next++] = x;          }} Private class Sequenceselector implements Selector {private int i = 0;          @Override public Boolean End () {return i = = items.length;}          @Override public Object Current () {return items[i];          } @Override public void Next () {if (I < items.length) i++;     }} public Selector Selector () {return new sequenceselector ();          } public static void Main (string[] args) {Sequence Sequence = new Sequence (10);          for (int i = 0; i < 10;i++) {Sequence.add (integer.tostring (i));        }  Selector Selector = Sequence.selector ();               while (!selector.end ()) {System.out.println (selector.current ());          Selector.next (); }     }}
. This and. NewBecause the inner class has direct access to the members of the host class, it itself has a reference to the host class. If you need to generate references to external class objects, you can use the names of the outer classes immediately following the dot and this.
public class Dotthis {     void F () {System.out.println ("dotthis.f ()");}     public class inner{public     dotthis outer () {          return dotthis.this;}     } Public Inner Inner () {return new Inner ();};     public static void Main (string[] args) {          dotthis dt = new Dotthis ();          Dotthis.inner dti = Dt.inner ();          Dti.outer (). f ();     }}
Sometimes you need to create an object of one of its inner classes, rather than returning it through a member method, in which case you must use an object from an external class to create an object of that inner class, using the. New syntax.
public class Dotnew {public     class inner{};     public static void Main (string[] args) {          dotnew DN = new Dotnew ();          Dotnew.inner dni = dn.new Inner ();}     }

Anonymous Inner classThe most significant difference between an anonymous inner class and an inner class is that because it is anonymous, you cannot arbitrarily go to the new object in any location, so it is often used for classes that are used only once, or for classes that can be new only somewhere (methods, object members). From this point of view it is the most restrictive control of class access: You can create classes only in a certain location.     The inner class can be created at least in the host class. Anonymous inner classes support direct access to local references to the methods in which they are located, and because the reference and base types are out of reach, the external objects in the method that require access in the anonymous inner class must be final.     However, the class member object accessed by the anonymous inner class does not have to be final. Because it is not possible to have a named constructor in an anonymous class (because it does not have a name at all), the effect of creating a constructor for an anonymous inner class can be achieved by initializing the code snippet.
Abstract class base{public     Base (int i) {          System.out.println ("Base constructor, i =" + i);     }     public abstract void F ();} public class Anonymousconstructor {     private static int w = 7;     public static base getbase (int i,final int j) {          return new base (i) {               {System.out.println ("Inside instance initial "); }               private int PJ = j;               private int pw = W;               public void F () {                    System.out.println ("in Anonymous F ()");               }          };     }     public static void Main (string[] args) {          base base = Anonymousconstructor.getbase (.);          Base.F ();     }}

Nested ClassesA nested class is also a class defined in a class, but it does not have a particularly direct relationship with the host class. In use form, an inner class is modified by static to become a nested class. In a nested class, you cannot directly access non-static members in the host class. Nested classes are rarely used in the actual work, since it is similar to the access rights of a common class to a host class and why should it be defined in a host class? A nested class, in turn, allows an external class to directly define the object of that nested class, which is more similar to the normal class.

Use of the Java inner class, anonymous inner class, nested class

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.