Inner classes in Java

Source: Internet
Author: User


/**
* Inner class??
* Defined in an internal class, called an inner class.
* Inner class has a class body, method body
* The class in which the inner class is located is called an external class.
* ---------------------------------
The meaning of *① inner class
* To a certain extent, the effective protection of the code.
*② syntax
* Class outer{////External class
* Member variables
* Member method
* Class inner{///Inner classes
* Member variables
* Member method
* }
* }
Creation of *③ class objects
*//Create an external class object
* Outer Outer =new Outer ();
*//Create inner class object
* Inner inner=new Inner ();//This creates the wrong
*//Cannot create an inner class object directly, but you can create an external class object by first
* Then create an inner class object from an external class object.
For example
* Outer outer=new Outer ();
* Outer.Inner inner2=new Outer (). New Inner ();
* OR
* Outer.Inner inner2=outer.new Inner ();
*
* The inner class references the member variables of the outer class
* Two cases
*a. The member variables of the external class do not exist in the inner class.
* The name of the member variable of the outer class can be used directly in the inner class.
*class Outer {
*string name;
*class inner{
*public void test{
*name= "CC";//is the name of the outer class;
*}
*}
*}
*b. The member variables of the outer class exist in the inner class, and the inner class wants to use the member variables of the outer class.
* Must have an external class name. This member variable name
*class outer{
*string name;
*class inner{
*string name;
*public void Test () {
*name= "CC";//is the name of the inner class;
*outer.this.name= "DD";//the name of the external class at this time
*}
*}
*}
*
* @param args
*/

Package test05;
public class Innerclass {

public static void Main (string[] args) {
TODO auto-generated method stubs
Outer outer=new Outer ();
Outer.name= "Zhang San";
outer.age=18;
Outer.introduce ();
System.out.println ("-------------------------------");
Outer.Inner inner=outer.new Inner ();
Inner.name= "John Doe";
inner.age=20;
Inner.introduce ();
}

}

Class outer{/////External class
String name;
int age;
void introduce () {
System.out.println ("name" +this.name+ "Age" +this.age);
}
Class inner{/////Inner Classes
String name;
int age;
void introduce () {
System.out.println ("Inner class name:" +this.name+ "inner class Age: \ n" +this.age+ "external class name:" +outer.this.name+ "outside class Age:" +outer.this.age ");
}

}
}

Inner classes in Java

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.