The inner class of Java

Source: Internet
Author: User

Inner classes in Java

In the literal sense we can know that the inner class (Inner Class) is the class defined in another class, and the class containing the inner class is called the outer class.

The main functions of the inner class are as follows:

    1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class

    2. The methods of the inner class can access all the data of the external class directly, including the private data

    3. Functions implemented by inner classes can also be implemented using external classes, but sometimes it is easier to use internal classes

Let's learn the inner class in detail by code!

member Inner class:
package com.test;//External Class     public class People {        private String name =  "Lilei";          //private properties of external classes     //inner class Student    public class  Student {        String ID =  "20151234";                //member properties of the inner class          //method of inner class         public  Void stuinfo () {            system.out.println ("Access name in external class:"  + name);             System.out.println ("Access ID in inner class:"  + id);        }     }    //Test member Inner Class     public static void main (String[] args)  {         people a = new people ();      //create an external class object with the object name a        student b = a.new  Student ();  //creates an inner class object with an external class object, with the object name B        b.stuinfo ();    //calls the Suinfo method of the internal object     }}

    1. student class is equivalent to a member variable of the People class, so the Student class can use any access modifier

    2. inner class object name = Outer class object. New inner Class ();

Static Inner class:
package com.test;//External Class peoplepublic class people {    private  string name =  "Lilei";          //private properties of external classes       /* static variables for external classes.     Java  members that are  static  decorated are called static members or class members. It is owned by the entire class, not by an object, which is shared by the class's      objects. Static members can be accessed directly by using the class name or by using the object name.     /*    static String ID =  "510xxx199x0724xxxx";      //static inner class student    public static class student  {        String ID =  "20151234";                //member properties of the inner class          //method of inner class         public void  Stuinfo () {        &NBsp;   system.out.println ("Access ID in external class:"  + people.id);             system.out.println ("Access ID in inner class:"  + id);         }    }    //Test Members internal class      public static void main (String[] args)  {         student b = new student ();    //directly creates an inner class object with the object name b         b.stuinfo ();                  //calls the Suinfo method of the internal object     }}

Static inner classes are internal classes of static adornments, which are characterized by:

  1. Static inner classes cannot directly access non-static members of external classes, but can new 外部类().成员 access them in a way

  2. If the static member of the outer class is the same as the member name of the inner class, you can access the static members of the outer class, or if the static members of 类名.静态成员 the outer class are not the same as the member names of the inner class, you can 成员名 call the static members of the outer class directly by calling

  3. When you create an object of a static inner class, you do not need an object of the outer class, you can directly create内部类 对象名= new 内部类();

Method Inner Class

The inner class of a method is an inner class that is defined in a method of an outer class, and the inner class of the method is visible only within the method, that is, it can be used only inside the method.

For example:

package com.test;//External Class peoplepublic class people {         //methods in external Classes     public void peopleinfo ()  {            final String sex =  "man";   //constants in external class methods            class Student {                String ID =  "20151234";  //constants in the inner class                 Public void print ()  {                    SYSTEM.OUT.PRINTLN ("Access constants in methods of external classes sex:"  + sex);                     SYSTEM.OUT.PRINTLN ("Accessing the variable ID in the inner class:" + id);                }            }         student a = new student ();   //create an object of a method inner class          a.print ();//method of calling inner class     }    //test method inner class      public static void main (String[] args)  {         people b = new people ();  //create an object of an external class          b.peopleinfo ();   //calls the method of the external class     }}

Operation Result:

650) this.width=650; "Src=" https://dn-anything-about-doc.qbox.me/ document-uid79144labid1072timestamp1434952802822.png?watermark/1/image/ AHR0CDOVL3N5BC1ZDGF0AWMUCWLUAXVKBI5JB20VAW1NL3DHDGVYBWFYAY5WBMC=/DISSOLVE/60/GRAVITY/SOUTHEAST/DX/0/DY/10 "alt= "Run result" style= "border:0px;vertical-align:middle;"/>


This article is from the "canvas shoes can walk the cat step" blog, please be sure to keep this source http://9409270.blog.51cto.com/9399270/1708683

The inner class of 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.