Member Inner classes in Java

Source: Internet
Author: User

Instructions for using members ' internal classes
    • The inner class can directly access the variables in the outer class and is not controlled by the access modifiers;
    • After defining the member inner class, you must use an external class object to create the inner class object, that is, the inner class object name = External class. New inner Class ();
    • After compiling the above program, you will find that there are two. class files//Where the second is the. class file for the outer class, and the first is the. class file for the inner class, that is, the. class file for the inner class of the member is always the case: external class name $ internal class name.
    • an external class cannot use the members and methods of an inner class directly, you can first create an object of the inner class, and then access its member variables and methods through the objects of the inner class.
    • If an external class and an inner class have the same member variable or method, the inner class accesses its own member variable or method by default, and if you want to access the member variables of the external class , you can use the This keyword .

External class HelloWorld

public class helloworld{

Private String name= "IMOOC";

int age=20;

public class inner{

String name= "Huangyl";

public void Show () {

System.out.println ("Name in the outer class:" +helloworld.this.name);

System.out.println ("Age in The Outer class:" +helloworld.this.age);

System.out.println ("Name in the Inner class:" +name);

}

}

Test member Inner Class

public static void Main (Sting () args) {

HelloWorld out=new HelloWorld (); //Create an object of an external class

Inner Iner = Out.new Inner (); //Create an object of the inner class

Iner.show ();

}

}

Member 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.