Use of Java internal classes

Source: Internet
Author: User

Inner class, a Class A is defined inside another class B, then we call Class A an inner class

1. member Inner class

Experimental research:
Create Class Test
public class Test {

public static void Main (string[] args) {

}

}
creates a class out representing an outer class, a class in representing an inner class, declaring various properties in a class
class out {//java is not recommended to use _ in naming to define variable names and method names, but for the sake of being intuitive, this is a temporary
and doing so.
int out_a;//defines an external class member variable
static int static_out_a;//defines an external class static member variable
void Out_print () {//define an external class member Method

}
static void Static_out_print () {//define an external class static member method

}
class in {
int in_a;//define an inner class member variable
//static int static_in_a; Define an inner class static member variable, see note ①
static final int final_in_a=10;//static final constant is allowed to be defined by
Void In_print () {

}
//static void Static_out_print () {///define an inner class static member method, see annotations
①

//}

}
}
Annotations ①: At this time, we found that Static variables and static methods are not allowed in member inner classes, but static
State constants can be defined
then we try to create an inner class object in the main method, and then enter in In=new in (), This time the program error, because the
for the member of the internal class object creation depends on the external class object , so the code is modified to
out = new Out ();
Out.in in = Out.new in ();

or out.in in=new out (). New in (); Compile succeeded.
An attempt was made to access the members of an external class in a method of an inner class, whether out_a or static_out_a, or a method that adds a
The variable of the private modifier can be accessed without error, and the inner class can access all members of the outer class, including
Any properties and methods.
Next, we declare the same string variable same and the same method print () in both the outer class and the inner class, both
The print () method prints the value of the variable: System.out.println ("In same String:" +same), and in the Main method
To create an Out object, note that you cannot directly come out of the new object, because then the original code points to the
Is the same object:
Out.in in=new Out (). New in ();
Out out=new out ();----> Error.
Revision changed to
Out out=new out ();
Out.in in=out.new in ();-----> Correct

Two objects each call a method
In.print ();
Out.print ();
The output is:
In same string:same
Out same string:same
The print () method in the inner class then adds the statement out.this.same= "Out_same"; Out.this.print (); Print again
Get:
In same string:same
Out same string:out_same
Out same string:out_same
This means that when you have variables and methods of the same name in the outer and inner classes, you can use the external class name. This is the form of the member name to indicate
It is a member of an object of an external class. However, this form can only be done in the body of the method and is non-static or static constants

(This is well understood, because inner classes can define static constants, but they cannot define non-static variables and methods)

The definition of a member's inner class can be added to the access control character:
Add private can only be accessed in this class
When adding protected different packages, only subclasses of the outer class can be accessed
Add public to access

 Public classTest { Public Static voidMain (string[] args) {//In In = new in ();//failed to create an inner class object, the creation of a member inner class object depends on the Outer class object//out = new Out (); //out.in in = Out.new in ();//Create an inner class object successfully//out.in in = new Out (). New in ();//Create an inner class object successfullyOut of out =Newout (); Out.in in=out.NewIn ();        In.print ();    Out.print (); }}classOut {//In Java, it is not recommended to use _ in naming to define variable names and method names, but for the sake of visualization, this is done for the moment.     intOut_a;//define an external class member variable    Static intStatic_out_a;//define an external class static member variable    Private intprivate_out_a; String same= "Same";//define attributes with the same name for an outer class and an inner class    Static Final intFinal_out_a = 9; voidOut_print () {//define an external class member method    }    voidPrint () {//defines the method with the same name for the outer class and inner classSystem.out.println ("Out same String:" +same); }    Static voidStatic_out_print () {//defining an external class static member method    }    classIn {intin_a;//define an intrinsic class member variable//static int static_in_a;//define an inner class static member variable        Static Final intFinal_in_a = 10;//static final constants are allowed to be definedString same = "Same"; voidIn_print () {private_out_a= 10; System.out.println ("Private_out_a:" +private_out_a); }        voidprint () {out. This. Same= "Out_same"; System.out.println ("In same String:" +same); Out. This. Print (); }        //Out.this.print ();//error, can only be called within a method    }}

2. Static Inner class

Let's take a look at this piece of code first

 PackageCom.epilogue.day1;classOut {intOut_a = 5; Static intOut_static_a = 5; Static classIn {Static intIn_static_a;//Note ①        Static voidIn_static_print () {//Note ①//System.out.println ("out_a =" +out_a);//error, annotation ②System.out.println ("out_static_a =" +out_static_a); System.out.println (Static methods for static internal classes); }        intb; voidIn_print () {//System.out.println ("out_a =" +out_a);//error, annotation ②System.out.println ("out_static_a =" +out_static_a); System.out.println ("Non-static methods for static internal classes"); }    }} Public classTest { Public Static voidMain (string[] args) {System.out.println ("Out.in.a =" + Out.In.in_static_a);//accessing static members of static internal classes, annotations ③Out.In.in_static_print ();//accessing static members of static internal classes, annotations ③Out.in in =NewOut.in ();//Create an inner class object, annotation ④System.out.println ("b =" + in.b);//to access a non-static member of a static inner class:In.in_print (); }}

Note ①: As you can see, a static inner class is a static member that can be defined

Note ②: Internal static internal classes cannot access non-static members of external classes: Static inner class objects do not depend on objects of external classes

注解③:当外部内要访问内部类的静态成员时,只需要外部类名.内部类名.静态成员名即可调用 Note ④: Creating an inner class object requires only the external class name. The internal class name Object name =new the external class name. Inner class name ()

Use of Java internal classes

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.