Use of j2se internal classes

Source: Internet
Author: User

Java has four internal classes:

1. static inner class: only static member variables and static methods of external classes can be accessed. The method for generating static internal class objects is as follows:

Outerclass. innerclass inner = new outerclass. innerclass ();

Example:

Public class staticinnerclass {
/**
* Use of static internal classes
*/
Public static void main (string [] ARGs ){

Staticinner. Inner inner = new staticinner. Inner ();
Inner. Test ();
}
}
Class staticinner {
Public static int A = 5;

Public static class inner {
Public void test (){
System. Out. println ();
}
}
}

2. Member internal class (member inner class): the static and non-static objects and methods of the external class can be accessed. The method for generating static internal class objects is as follows:
Outerclass. innerclass inner = new outerclass (). New innerclass ();

Example:

/**
* Member internal class
*/
Public class memberinnerclass {
Public static void main (string [] ARGs ){
Memberinner. inner2 inner2 = new memberinner (). New inner2 ();
Inner2.dosomething ();
}
}

Class memberinner {
Private int A = 4;
Public class inner2 {
Private int A = 5;
Public void dosomething (){
System. Out. println (memberinner. This. );
}
}
}

To access the member variables of an external class in a local internal class, the syntax is: outerclass. This.

3. Local inner class)

Defined in a method, only final variables declared in the method can be accessed.

Example:

Public class localinnerclass {
/**
* Local internal class
*/
Public static void main (string [] ARGs ){
Localinner inn = new localinner ();
Inn. dosomething ();
}
}

Class localinner {
Public void dosomething (){
Final int A = 4;
Class inner3 {
Public void test (){
System. Out. println ("Hello World ");
System. Out. println ();
}
}
New inner3 (). Test ();
}
}

 

4. Anonymous internal class (anonymous inner class) (most used)

An anonymous internal class implicitly inherits a parent class or implements an interface.

Example:

Public class anonymousinnerclass {
/**
* Anonymous internal class
*/
Public static void main (string [] ARGs ){
Anonymousinnerclass AIC = new anonymousinnerclass ();

String STR = AIC. getdate (new date (){});
// Generate an object that inherits the subclass of the date class
System. Out. println (STR );
}

Public String getdate (date ){
Return date. tolocalestring ();
}
}

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.