JAVA_SE basics-51. Internal class

Source: Internet
Author: User

JAVA_SE basics-51. Internal class

In Java, classes can be defined inside a class. Such classes are called internal classes, and the classes of these internal classes are called external classes. Based on the position, modifier, and definition of the internal class, it can be divided into the internal class of the member, the static internal class, and the internal class of the method (Local internal class.

 

Internal class: A class is defined inside another class, and the class is called an internal class.

Class File Name of the internal class: External class $ internal class. Benefit: it is easy to distinguish which external class the class file belongs.

 

 

 

Member internal class

 

 

In addition to member variables and member methods, a class can also be defined. Such a class is called a member internal class.

Internal class access method:

Method 1: an external class provides a method to create an internal class object for access. Code1 uses this method

Method 2: directly create internal class objects in other classes. Code2 uses this method

Format: External class. Internal class variable name = new external class (). new internal class ();

 

Code1

 

Class A {int num = 10; // The following Code defines a member method. The internal class public void test () {B a = new B ();. show () ;}// the following code defines a member's internal class B {public void show () {System. out. println (num = + num) ;}} class Demo1 {public static void main (String [] args) {A a = new A ();. test ();}}
Running result:

 

Num = 10

 

Code2

 

Class A {int num = 10; // The following Code defines A member's internal class B {public void show () {System. out. println (num = + num) ;}} class Demo2 {public static void main (String [] args) {. B a = new (). new B ();. show ();}}
Running result:

 

Num = 10

 

 

Benefits of the internal class: the internal class can directly access all members of the external class.

 

 

 

 

Static internal class

You can use the static keyword to modify the internal class of a member. This internal class is called a static internal class, it can be instantiated without creating external class objects (because static internal classes are loaded with external classes ).

 

Static internal class, the format created in other classes:

External class. Internal class variable name = new external class. Internal class (); code3 uses this method

Code3

 

Class A {// defines the static member variable static int num = 10; // The following Code defines A static internal class static class B {public void show () {System. out. println (num = + num) ;}} class Demo3 {public static void main (String [] args) {. B a = new. B ();. show ();}}

 

Running result:

Num = 10

 

P.S.

In a static internal class, you can only access static methods of the external class. If you remove the static keyword of the static member num of the external class, an error is returned.

Static members can be defined in static internal classes, but static members cannot be defined in non-static internal classes. (Because static members in the internal class are loaded with the internal class, but this is against what sun originally defined: static member data does not need to be accessed by objects .)

 

 

 

 

Method internal class (Local internal class)

An internal class of a method is a class defined in the member method. It can only be used in the current method.

Local internal class: defines another class in the method of a class, and the other class is called as a local internal class.
Pay attention to the details of local internal classes:

The local internal class can access the member variables of the external class, And code4 implements this sentence.


If a local internal class accesses a local variable, the local variable must be modified using final (an error will be reported if the local variable below java1.8 is not modified using final) and (My java1.8 has been tested, there will be no error even if there is no final. It is estimated that it will be optimized ..)

 

 

 

Code4

 

Class A {int num = 10; // The following Code defines A member's internal class public void test () {class B {public void show () {System. out. println (num = + num) ;}// because Class B is an internal class of the method, you can only create an instance object for this class in the method and call show () method, B B = new B (); B. show () ;}} class Demo4 {public static void main (String [] args) {A a = new A ();. test ();}}
Running result:

 

Num = 10

 

An internal class B is defined in the test () method of Class A. Because Class B is the internal class of the method, therefore, the program can only create instance objects of this class in the method and call the show () method. From the result, the internal class of the method can access the member variables of the external 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.