Referencing an external class object

Source: Internet
Author: User

To generate a handle to an external class object, you use a dot number and a this to name the outer class. For example, in the Sequence.sselector class, all of its methods can produce a stored handle to an external class sequence, using the form of sequence.this. The resulting handle will automatically have the correct type (this is checked and verified during compilation, so there is no runtime overhead).
Sometimes we want to tell some other objects to create an object of one of its inner classes. To do this, you must provide a handle to other external class objects in the new expression, as follows:

: Parcel11.java
//Creating inner Classes
package c07.parcel11;

public class Parcel11 {
  class Contents} {
    private int i = one;
    public int value () {return i;}
  }
  Class Destination {
    private String label;
    Destination (String whereto) {
      label = Whereto;
    }
    String Readlabel () {return label;}
  }
  public static void Main (string[] args) {
    Parcel11 p = new Parcel11 ();
    Must use instance of Outer class
    //To create a instances of the inner class:
    parcel11.contents c = p.new Co Ntents ();
    Parcel11.destination d =
      p.new destination ("Tanzania");
  }
///:~

To create an object for an inner class directly, you cannot use the same form as you might guess, and reference the external class name PARCEL11. At this point, an object of an inner class must be generated from an object of an external class:
Parcel11.contents C = p.new Contents ();
Therefore, it is not possible to create an object of an inner class unless you already have an object of an external class. This is because the object of the inner class has been "silently" connected to the object of the external class that created it. However, if you generate a static inner class, you do not need to point to a handle to the external class object.

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.