Java Inner class: member inner Class (II)

Source: Internet
Author: User

The Java inner class is divided into 4 sections, namely, overview, member inner class, local inner class, and anonymous inner class.


In this article, it is the inner class of a member of the Java inner class that mainly describes how the members ' internal classes are accessed and some details.

    • member Inner class

    1. How member-Internal classes are accessed

      1. Non-static member inner class

      2. Static member Inner class

      1. An external class provides a way to create an object of an inner class

      2. Directly create an object of the inner class

    2. Details of the member's inner class to be aware of



1. How to access members ' internal classes


The way in which a member's inner class is accessed can also be understood as how it is created, mainly in two ways: the first is to provide a method for creating an object of an inner class by an external class, and the second method to directly create an object of the inner class.


1.1. Provide a method to create an inner class object


In the following code, a Getinnerclassinstance method is provided by Outerclass to create an object of the Innerclass inner class.

Example code:

Outerclass outter = new Outerclass (); Outerclass.innerclass inner = outter.getinnerclassinstance ();

Full code:

Package Com.rk.innerclass;class outerclass{class innerclass{void print () {System.out.println (" I am the print method of the Innerclass internal class ");}} Public Innerclass getinnerclassinstance () {Innerclass instance = new Innerclass (); return instance;}} public class Memberinnerclass{public static void Main (string[] args) {Outerclass outter = new Outerclass (); O Uterclass.innerclass inner = outter.getinnerclassinstance (); Inner.print ();}}



1.2. Directly create the object of the member inner class


There are two cases of directly creating an object of a member's inner class: one is when a member's inner class is static decorated, and the other is not modified by static.


1.2.1, when a member inner class is not modified by static


When a member inner class is not modified by static, it is created in the following format:

External class. Internal class variable name = new external Class (). New inner Class ();

Example code:

Outerclass outter = new Outerclass (); Outerclass.innerclass inner = outter.new innerclass ();

Full code:

Package Com.rk.innerclass;class outerclass{class innerclass{void print () {System.out.println (" I am the print method of the Innerclass internal class ");}}} public class Memberinnerclass{public static void Main (string[] args) {Outerclass outter = new Outerclass (); O Uterclass.innerclass inner = outter.new innerclass (); Inner.print ();}}




1.2.2, when a member inner class is static decorated


When a member inner class is static decorated, it is created in the form:

External class. Internal class variable name = new External class. Inner class ();

Example code:

Outerclass.innerclass inner = new Outerclass.innerclass ();

Full code:

Package Com.rk.innerclass;class Outerclass{static class innerclass{void print () {System.out.println (" I am the print method of the Innerclass internal class ");}}} public class Memberinnerclass{public static void Main (string[] args) {Outerclass.innerclass inner = new Outerclass.innerclass (); Inner.print ();}}



2, members of the internal class to pay attention to the details



2.1. Detail one: When "member Inner class" and "external class" have members of the same name


If an external class has a member variable with the same name as an inner class, the member variable of the inner class is accessed by default in the inner class. You can specify access to members of external classes through the external class. this. member variable name.


You can access the same name "member variable" of the external class through "external class. This"

Example code:

Outerclass.this.x

Full code:

Package Com.rk.innerclass;class outerclass{private int x = 100;class innerclass{private int x = 200;void print () {System.ou T.println (the value of X for the Outerclass class is: "+ outerclass.this.x);//100system.out.println (the value of X for the Innerclass class is:" + this.x);//200}}} public class Memberinnerclass{public static void Main (string[] args) {Outerclass outter = new Outerclass (); O Uterclass.innerclass inner = outter.new innerclass (); Inner.print ();}}


"Member methods" with the same name as the external class can be accessed through the "external class. This"

Example code:

OuterClass.this.test ();

Full code:

Package Com.rk.innerclass;class outerclass{void Test () {System.out.println ("This is the test method for the Outerclass class");} Class Innerclass{void print () {OuterClass.this.test (); Test ();} void Test () {System.out.println ("This is the test method of the Innerclass class");}}} public class Memberinnerclass{public static void Main (string[] args) {Outerclass outter = new Outerclass (); O Uterclass.innerclass inner = outter.new innerclass (); Inner.print ();}}



2.2. Detail two: When the member inner class is modified by private


Private members inner classes can only be accessed in an external class that provides a method for creating an object of an inner class, and cannot create an object in another class.


package com.rk.innerclass;interface imath{void add (int  A,INT B);} Class outerclass{void calculate (int a,int b) {innerclass inner = new  Innerclass (); Inner.add (A, b);} Innerclass getinnerclassinstance () {innerclass instance = new innerclass (); return  instance;} private class innerclass implements imath{@Overridepublic  void add (Int a,  INT B) {System.out.println (a +  " + "  + b +  " = "  +  (A+B);}}} Public class memberinnerclass{public static void main (String[] args) {OuterClass  outter = new outerclass (); Outter.calculate (;imath inner = ) Outter.getinnerclassinstance (); inner.add;}} 



2.3. Detail three: When a member inner class contains a static member


    Once a member's inner class has a static member, the class must also use the static modifier.

Class Outerclass{static class innerclass{static double PI = 3.14;}}

In the preceding code, the PI is declared as static, and the Innerclass is also required to be declared as static.


3. Mind Mapping

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/02/wKiom1cPcVHRErbPAAC1lMLBRUI355.png "title=" Java internal class. png "alt=" Wkiom1cpcvhrerbpaac1lmlbrui355.png "/>



Java Inner class: member inner Class (II)

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.