Java inner class and static inner class

Source: Internet
Author: User

See Code understanding Inner class, local inner class and static inner class

Internal Class Code:

 Public classInnertest {Private StaticString name; Private intAge ;  PublicInnertest (String name,intAge ) {         This. Name =name;  This. Age =Age ; }     Public voidgetinstance () {System.out.println ("Name:" + name.tostring () + ", Age:" +Age ); }    classA {PrivateString sex;  PublicA (String sex) { This. Sex =sex; }         Public voidgetinstance () {System.out.println (sex.tostring ()+name.tostring ()); }    }     Public Static voidMain (string[] args) {innertest innertest=NewInnertest ("Zhang San", 25);        Innertest.getinstance (); innertest.a ia = innertest.new A ("male");//Instantiate Object format for inner classia.getinstance (); }}

Local inner class:

 Public classInnertest {Private StaticString name; Private intAge ;  PublicInnertest (String name,intAge ) {         This. Name =name;  This. Age =Age ; }     Public voidgetinstance () {System.out.println ("Name:" + name.tostring () + ", Age:" +Age ); }     Public Static classA {PrivateString sex;  PublicA (String sex) { This. Sex =sex; }         Public voidgetinstance () {classb{PrivateString like;  PublicB (String like) { This. like =Like ; }                 PublicString getlike () {return  This. like.tostring (); }} System.out.println (Sex.tostring ()+ name.tostring () + ", like:" +NewB ("Basketball"). Getlike ()); }    }     Public Static voidMain (string[] args) {innertest innertest=NewInnertest ("Zhang San", 25);        Innertest.getinstance (); INNERTEST.A ia=NewA ("male");//instantiating object formats for inner classesia.getinstance (); }}

Static Inner class:

 Public classInnertest {Private StaticString name; Private intAge ;  PublicInnertest (String name,intAge ) {         This. Name =name;  This. Age =Age ; }     Public voidgetinstance () {System.out.println ("Name:" + name.tostring () + ", Age:" +Age ); }     Public Static classA {PrivateString sex;  PublicA (String sex) { This. Sex =sex; }         Public voidgetinstance () {System.out.println (sex.tostring ()+name.tostring ()); }    }     Public Static voidMain (string[] args) {innertest innertest=NewInnertest ("Zhang San", 25);        Innertest.getinstance (); INNERTEST.A ia=NewA ("male");//instantiating object formats for static inner classesia.getinstance (); }}

The following is the difference between an internal class (Inner Class) and a static inner class (Nested classes):
A class defined inside a class is called an inner class, and the class containing the inner class is called an external class. An inner class can declare access restrictions such as public, protected, private, and can be declared as abstract for other inner classes or external classes to inherit and extend, or to declare static, final, or to implement a specific interface. External classes use internal classes in general class access, with the only difference being that external classes can access all the methods and properties of the inner class, including private methods and properties .

(1) Create an instance

Outclass.innerclass obj = outclassinstance.new innerclass (); Note the external class instance. New, Inner class

Aaa. Staticinner in = new AAA. Staticinner ();//note is the outer class itself, static inner class

(2) This in the inner class

This in the inner class is the same as other classes. When you create an inner class object, it has a connection to the perimeter object that created it, so you can access all members of the perimeter class without any special conditions that you can understand as an inner class that is linked to an external class. When you create an inner class object with an external class, the inner class object secretly captures a reference to the outer class, and you can access the members of the perimeter class through this reference.

(3) External class access inner class

Inner classes are similar to properties of external classes, so it is always necessary to create an external class object when accessing an inner class object. The inner class object accesses the properties and methods of the external class through the form ' external class name. this.xxx '. Such as:
System.out.println ("Print in inner outer.index=" + pouter.this.index);
System.out.println ("Print in inner inner.index=" + this.index);

(4) Internal class upward transformation

Inner classes can also have an upward-shifting nature as normal classes. When you move an inner class up to a base type, especially an interface, the inner class comes into play. If the inner class is private, it can only be asked by its external class, thus completely hiding the implementation details.

(5) class within the method

A class created within a method (note that a class can also be defined in a method) and cannot be accessed with modifiers. Also, the classes inside the method are not created when the method is called, and they are compiled in advance.

(6) Static inner class

Define a static inner class: When you define an inner class, you can precede it with a permission modifier static. At this point, the inner class becomes a static inner class.

Often called a nested class , when an inner class is static, it means:

[1] To create the object of a nested class, it does not require the object of its peripheral class;

[2] Non-static peripheral class objects cannot be accessed from objects in nested classes (non-static members of external classes cannot be accessed from objects in static inner classes);

There is another difference between nested classes and ordinary inner classes: The fields and methods of ordinary inner classes can only be placed on the outer level of the class, so normal inner classes cannot have static data and static fields, nor can they contain nested classes. But in nested classes you can include all of these things. That is, a static member cannot be declared in a non-static inner class, only if an inner class is decorated as a static class, then static member variables and member methods can be defined in this class.

In addition, you do not need to bind an instance of a static inner class to an instance of an external class when creating a static inner class. The object of a normal non-static inner class is attached to an outer class object, and to define a static inner class within an external class, you do not need to use the keyword new to create an instance of the inner class. Static classes and methods belong only to the class itself, not to objects of that class, and to objects that are not part of other external classes.

(7) Inner class identifier

Each class produces a. class file, and the file name is the class name. Similarly, an inner class produces such a. class file, but its name is not the class name of the inner class, but it has strict restrictions: the name of the perimeter class, plus $, plus the inner class name.

(8) Why use internal partial?

1. Internal classes are generally used only for their outer classes;

2. Internal classes provide some kind of window into the outer class;

3. Also the most compelling reason, each inner class can inherit an interface independently, regardless of whether an external class has inherited an interface. Therefore, the inner class makes the solution for multiple inheritance more complete.

Java inner class and static 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.