Java within the class of the primary application of the detailed (with source)

Source: Internet
Author: User

Sample source Code
In this section we will cover a more typical scenario in an internal class application: An external class will have a method that returns a reference to an inner class, as in To ()And contents ()method as seen in the.
Package Com.mufeng.thetenthchapter;public class Parcell2 {class Contents {private int i = 11;public int value () {return i; }}class Destination {private string Label;public Destination (String whereto) {//TODO auto-generated constructor Stublabe L = whereto;} String Readlabel () {return label;}} Public Destination to (String s) {return new Destination (s);} Public Contents Contents () {return new Contents ();} public void ship (String dest) {Contents c = Contents ();D estination d = to (dest); System.out.println (C.value ()); System.out.println (D.readlabel ());} public static void Main (string[] args) {Parcell2 p = new Parcell2 ();p. Ship ("Tasmania"); System.out.println ("--------I am the dividing line----------"); PARCELL2 q = new Parcell2 (); Parcell2.contents C = q.contents ();//(or Q.new Contents ()) parcell2.destination d = q.to ("Borneo");//(or q.new//Destinat Ion ("Borneo")) System.out.println (C.value ()); System.out.println (D.readlabel ());}}

Output results
11Tasmania--------I'm a split line----------11Borneo

SOURCE parsing
If you want to create an object of an inner class from anywhere other than the non-static method of the outer class, you must like the Main ()method, specifically indicating the type of the object: Outerclassname.innerclassname



Java within the class of the primary application of the detailed (with source)

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.