member inner class of Java inner class

Source: Internet
Author: User

Inner class concept: As the name implies, a class is defined inside another class. The inner class is called an inner class.

The main features of the inner class are:
1. Inner classes can be very well hidden and can use protected, private modifiers.
2. An inner class can access all members of an external class directly, including private members .
3. The external class cannot access the members of the inner class directly , and must first establish an object of the inner class to be accessible.
4. Internal classes can solve some problems, such as indirectly implementing inheritance. You can avoid modifying interfaces and implementing the same class
The invocation of two methods in the same name

There are four types of internal classes:

member inner class, Static inner class, anonymous inner class, local inner class

The $ symbol is a valid character when it is named, and is intended for the inner class

Internal class characteristics of members:
1. The member inner class belongs to the instance member of the external class, and the member inner class can have public,private,default.
The protected permission modifier. To access member methods and properties of an external class within a member's inner class, use the external
Class name. This. member method and external class name. This is the form of the member property.
2. Create an instance of a member's inner class using the external class name. Internal class Name Instance name = External class instance name. New Internal
The form of a class constructor method (parameter).
Restrictions for members inner classes:
1. The member inner class cannot duplicate the name of the external class
2. Static properties, methods, and classes cannot be defined in a member's inner class (except for the static final form of shade definitions) because
An internal class instance for a member must be associated with an instance of an external class, and the static member can be moved entirely to its outer class
To.

public class Memberinnerclassdemo {public static void main (string[] args) {//TODO auto-generated method StubOuter1 Outer1 = new Outer1 (); Outer1. Inner1 inner1 = Outer1.new Inner1 (); Outer1.outer (); Inner1.inner ();}} Class Outer1{private String name = "Zhang San";p rivate int num1 = 1;public void outer () {System.out.println (name+ "" +num1);//syst Em.out.println (num2); The outer class cannot directly access the Inner class member Inner1 Inner1 = new Inner1 ();//The outer class can instantiate an inner class, access the Inner class Inner1.inner ();}  Class inner1{//If the inner class is declared as private, the external class cannot be accessed by the inner class private String name = "John Doe";p rivate int num2 = 2;//private static final int NUM3 = 3; You can declare a static constant//private static int num3 = 3 in a member's inner class, or a static member in a member's inner class, including properties and methods public void inner () {System.out.println (nam E+ "" +num2); System.out.println (outer1.this.name+ "" +outer1.this.num1);//The inner class calls the external Class property outer ();//The inner class can directly access the properties and methods of the outer class. Include private Outer1.this.outer ();//As above, the difference is that if the internal class appears with the same name, then this can be resolved}}}

  

member inner class of Java inner class

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.