[Think in Java] Chapter 10th inner class

Source: Internet
Author: User

Inner class

Definition: A class is defined inside another class, which is an inner class.


Create an inner class

If you want to create an object of an inner class from anywhere other than the non-static method of the outer class, you need to specify: outerclassname.innerclassname;


linking to an external class

The inner class has access to all members of its perimeter object and does not require any special conditions.


use the. This and. New. This usage

If you need to generate a reference to an external class object, you can immediately follow the dot and this after the names of the outer classes. This is known and checked at compile time, so there is no runtime overhead. Look at the following example:

public class Dotthis {void F () {System.out.println ("dotthis.f ()");} public class Inner{public Dotthis outer () {return dotthis.this;  here//a plain "This" would is Inner ' s this}}public Inner Inner () {return new Inner ();} public static void Main (String [] args) {dotthis dt = new Dotthis ();D otthis.inner dti= dt.inner ();d ti.outer (). f ();} OUTPUT:DOTTHIS.F ()

. New Usage

To create an inner class object, you need to do so with a reference to the Outer class object followed by. New. Here's an example:

public class Dotnew {public class inner{}public static void Main (String [] args) {dotnew DN = new Dotnew ();D Otnew.inner dni = Dn.new Inner ();//Note: When creating an inner class with. New, use the external class object DN instead of the name of the outer class Dotnew}}

because an external class object is required to create an inner class, it is not possible to create an inner class object before owning an external class object. One exception, however, is that the inner class created is a static inner class (note here, the static inner class, not the static external class why?). )。


inner classes within methods and scopes

You can define an inner class at a method or any scope. Look at one of the following examples:

public class Parcel5 {//Here Destination is an interface  specific definition not given public Destination Destination (String s) {class pdestination Implements Destination{private string Label;private pdestination (string whereto) {label = Whereto;} Public String Readlabel () {return label;}} return new Pdestination (s);} public static void Main (String [] args) {Parcel5 p = new Parcel5 ();D estination d = p.destination ("Tasmania");}}

The Pdestination class is part of the destination method and is the inner class of the method. Cannot access pdestination outside of the destination () method. The return statement is transformed upward. In addition, the internal class pdestination defined in destination does not mean that the Dest () method is finished and pdestination is not available.


Anonymous Inner class

Let's look at an example:

public class Parcel7 {public Contents Contents () {//Here Contents is an interface  specific definition not given return new Contents () {//insert a class Def initionprivate int i = 11;public int value () {return i;}};} public static void Main (String [] args) {Parcel7 p = new Parcel7 (); Conents C = p.contents ();}}
The anonymous inner class is used in the contents method. When you create a Content object, you insert a definition of the anonymous inner class. If you want it to use an object that is defined externally in an anonymous inner class, the compiler will require that its parameters be final .

Anonymous inner classes are somewhat restricted compared to formal inheritance, because anonymous inner classes can extend or extend the interface, but not both, and if you implement an interface, you can only implement one interface .

[Think in Java] Chapter 10th inner 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.