member inner class, static inner class of Java inner class

Source: Internet
Author: User

the inner class is actually a class that also contains another class
As a person is composed of body results, such as the brain, limbs, organs, and the inner class is equivalent to one of the organs, such as the heart: it also has its own properties and behavior (blood, beating) obviously, here can not unilaterally use properties or methods to express a heart, but need a class, and the heart in the body, Just as the same inner class is inside the outside

The broad range of internal classes generally includes these four types: member inner class, local inner class, anonymous inner class, and static inner class. Let's take a look at the usage of these four inner classes.

First , member Inner class: The member inner class is the most common inner class and is defined as being inside another class , the member inner class can unconditionally access all member properties and member methods of the external class, including private and static members.
Outer classes class out {    private int-age = n;         Inner class    ' {public        void ' print () {System.out.println (age)}} ' public        class '    Demo {    public static void Main (string[] args) {        out.in in = new Out (). New in ();        In.print ();        or Access/        * out with sowing        = new Out ();        Out.in in = Out.new in ();        In.print ();        */    }}
Run Result: 12
From the above example, it is not difficult to see that the inner class actually seriously destroys the good code structure, but why use the inner class?
Because inner classes are free to use member variables of external classes (including private ones) without generating objects of external classes, this is the only advantage of inner classes

Like the heart can directly access the body's blood, rather than through the doctor to draw the blood

After compiling the program, two. class files are generated, respectively Out.class and Out$in.class
Where $ represents the out.in in the above program.
Out.in in = new Out (). The new in () can be used to generate an object of the inner class, which has two small knowledge points to note
1. The first out is to indicate which outer class the inner class object needs to be generated
2. An object of an outer class must be preceded to generate an object of the inner class, because the inner class is intended to access member variables in the outer class

variable Access forms in the inner class:      

Class out {    private int: age = n;         Class in {        private int: age =;        public void print () {            INT-age = +;            SYSTEM.OUT.PRINTLN ("local variable:" + age);            System.out.println ("Inner class variable:" + this.age);            SYSTEM.OUT.PRINTLN ("External class variable:" + Out.this.age);}}}    public class Demo {public    static void Main (string[] args) {        out.in in = new Out (). New in ();        In.print ();    }}
Operation Result:
Local variables: 14
Internal class variable: 13
External class variables: 12

Attention:

1 . The member inner class can unconditionally access all member properties and member methods of the external class, including private and static members.

2, when the member inner class has a member variable or method with the same name as the outer class, a hidden phenomenon occurs, that is, by default, members of the members inner class are accessed. If you want to access a member of the same name as an external class, you need to access it in the following form:

External class. This. member variable external class. This member method
3, although members of the inner class can unconditionally access the members of the external class, and the external class would like to access members of the inner class member is not so arbitrary. In an external class, if you want to access members of a member's inner class, you must first create an object of the member's inner class, and then access it by referring to the object
4, the member inner class is dependent on the external class, that is, if you want to create an object of the member inner class, if there must be an object of an outer class.

5, the internal class can have private access rights, protected Access, public access and package access rights. If the member inner class inner is decorated with private, it can only be accessed inside the external class, if it is modified with public, it will be accessible anywhere, if it is decorated with protected, it can only be accessed under the same package or inheriting the external class, if it is the default access permission. can only be accessed under the same package. This is a bit different from the external class, and the outer class can only be decorated with both public and package access permissions. Because the member inner class looks like a member of an external class, you can have multiple permissions adornments like members of a class.

static inner class: the static inner class is also a class defined in another class, except for a keyword static in front of the class. A static inner class is not dependent on an external class, which is somewhat similar to a static member property of a class, and it cannot use a non-static member variable or method of an external class, because in the absence of an object of an outer class, you can create an object of the static inner class. If you allow access to non-static members of an external class, there is a contradiction because the non-static members of the outer class must be attached to the specific object .

public class Test {public    static void Main (string[] args)  {        Outter.inner Inner = new Outter.inner ();}    } C Lass Outter {public    outter () {             }         static class Inner {public        Inner () {                     }}    }
Usage scenarios for static classes:
(1) External classes need to use internal classes, while internal classes do not need to use resources from external classes
(2) An inner class can create an object independently of an external class
The advantage of using static internal classes is that it enhances the encapsulation of the code and improves the readability of the code, for example:
public class person{         //name         private String name;         Home         Private home home;         The constructor sets the property value to public person         (String _name) {              name = _name;         }         /* Home, name Getter/setter method omitted */public            static class home{              //family address              private String address              ; Home Phone              Private String tel;                 Public Home (String _address,string _tel) {                address = _address;                tel = _tel;              }              /* Address, Tel's Getter/setter method omitted */         }    }
Put your home in person, and you'll see that home is a property of person. The use of the time is also very convenient, the following code:
public static void Main (string[] args) {        home home = new Person.home ("Shanghai", "021");      person P1 = new Person ("Zhang San");        person P2 = new Person ("John Doe");        P1.sethome (home);        P2.sethome (home);    }   
This creates a home object where both P1 and P2 use the home object, P1 and P2 share the same home object.
If you change home into a normal inner class, the situation is different, and then the code becomes:
public static void Main (string[] args) {person        p1 = new Person ("Zhang San");        Home home = P1.new Home ("Shanghai", "021");      P1.sethome (home);        person P2 = new Person ("John Doe");        P2.sethome (home);    

Here P1 and P2 still share the same home object, but at this time the home object and P1 are die, if the P1 object dies, then P2 will not
Home, this is really unfair to P2, and if you create a home object for P1 and P2, you waste resources. So in this case,
It is appropriate to use static internal analogies for ordinary internal classes.
the difference between a static inner class and a normal inner class
(1) Ordinary inner classes cannot declare static methods and variables
Ordinary inner classes cannot declare static methods and variables, note that here is the variable, constant (that is, the final static decorated property)
is still possible, while the static inner class resembles the outer class without any restrictions.
(2) with static inner classes, objects of multiple external classes can share objects of the same inner class.
With ordinary inner classes, objects of each external class have their own inner class objects, and external objects cannot share objects of inner classes

member inner class, static inner class of Java inner class

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.