Java internal class, Java

Source: Internet
Author: User

Java internal class, Java

Package innerclass; // in Java, a class can be defined in another class or method. Such a class is called an internal class. In a broad sense, internal classes generally include the following four types: member Internal classes, partial internal classes, anonymous internal classes, and static internal classes public class Outter {/* private can modify data members, constructor, method Member, cannot modify class (this refers to external class, do not consider internal class) protected can modify data member, constructor, method member, class cannot be modified (this refers to an external class without considering the internal class ). An internal class (static/member) can be viewed as a member of an external class. Therefore, it can have private access permission, protected access permission, public access permission, and default (package access permission) a local internal class is like a local variable in a method. It cannot contain */static int a = 1; int B = 5 with public, protected, private, and static modifiers; static void t1 () {System. out. println ("static external class method");} void t2 () {System. out. println ("non-static method of external class");}/* 1. static internal class: as a static member of a class, it exists inside a class. Although static internal classes are members of external classes, you can directly create static internal class objects without creating external class objects. Static internal classes can reference static member variables and static methods of external classes, but cannot reference common members of external classes. */Static class Inner1 {public void test1 () {System. out. println ("directly access the static member variable of the external class: the value of a is" + a); // directly reference the static member variable t1 () of the external class (); // directly reference the static method of the external class // System. out. println ("B's value is" + B); // attempts to reference non-static member variables of the external class, which cannot be compiled // t2 (); // try to reference the non-static method of the external class. out. println ("cannot be accessed directly, only new external class () can be used (). members access non-static members of the external class: the value of B is "+ new Outter (). b); // The static internal class cannot directly access non-static members of the external class, but can use the new external class (). new Outter (). t2 (); System. out. println ("static internal class method ~~~~~~~~~~~~~~ ~ ") ;}}/* 2. Member internal class: As a member of the class, it exists within a class. The member internal class can call all attributes and member methods of the external class (including private and static members), but only after the object of the external class is created, to create an object of the member's internal class. Therefore, the internal class of the Member cannot contain static methods and static member variables, because static variables occupy the memory, as long as they are defined as static variables during compilation, the system will automatically allocate the memory to the member, which means that the internal class of the member is compiled by external class compilation. That is to say, the internal class can only exist after the external class exists, because the system runs: run the external class-> static variable memory allocation-> internal class, and the static variables of the internal class are generated before the internal class. This is obviously impossible, so static variables cannot be defined! */Class Inner2 {public void test2 () {int a = 20; int B = 28; System. out. println ("to access members of the same name of an external class, you must use the external class (). this. static member variable: the value of a is "+ Outter. this. a); // access the static member variable System of the external class. out. println ("the hidden phenomenon accesses the static member variable of the member's internal class: the value of a is" + ); // when a hidden phenomenon occurs, access the member t1 () of the member's internal class; // call the static method System of the external class. out. println ("to access members of the same name of an external class, you must use the external class (). this. non-static member variable mode: the value of B is "+ Outter. this. b); // access the non-static member variable System of the external class. out. println ("hidden access refers to non-static member variables of the member's internal classes: the value of B is" + B); // hidden access Ask the member t2 () of the member internal class; // call the non-static method Outter of the external class. this. t2 (); System. out. println ("method of member Internal classes ~~~~~~~~~~~~~~~ ");}/* When a Member's internal class has a member variable or method with the same name as the external class, it is hidden. By default, the member accesses the Member's internal class. To access a member of the same name as an external class, access is required: External class (). this. member */void t2 () {System. out. println ("non-static methods of member Internal classes");}/* 3. local internal class: exists inside a method. The local internal class can only be used inside the method. Once the method is executed, the local internal class will be deleted from the memory. Note: If you want to use the local variables in the method of the local internal class, you need to define this local variable as final. The difference between the class and the member internal class is that the access to the local internal class is limited to the method or the scope */public void test () {final int c = 5; System. out. println ("external class method"); class Inner3 {void test3 () {System. out. println ("the value of static member variable a that directly accesses the external class is" + a); // The static member variable t1 () that accesses the external class (); // call the static method System of the external class. out. println ("the value of the non-static member variable B that directly accesses the external class is" + B); // The non-static member variable t2 () that accesses the external class (); // call the non-static method System of the external class. out. println ("access the local variable in the method where the local variable is located (must be modified using final): the value of c is" + c); // note: if you want to use the local variable in the method in the local internal class, you need to define this local variable as final (when the local internal class accesses the local ). System. out. println ("Local internal class method ~~~~~~~~~~~~~~~ ") ;}} Inner3 inner3 = new Inner3 (); inner3.test3 (); // The local internal class can only be used inside the method} public void Out () {// if you want to access members of an internal class in an external class, you must first create an object of the internal class of the member, and then access new Inner2 () by referencing the object (). test2 (); new Inner1 (). test1 ();} public static void main (String [] args) {Inner1 in1 = new Inner1 (); // you can directly create static internal class objects, you do not need to create the object in1.test1 () for the external class first; // Inner2 in = new Inner2 (); the object for the internal class of the member cannot be directly created, and an error occurs when Outter out1 = new Outter (); // first create the object Inner2 in2 = out1.new Inner of the external class 2 (); // Note :!! The object of the member's internal class must create in2.test2 (); out1.test ();/* 4. anonymous internal class: a class that exists inside a class but has no class name. 1) An anonymous internal class cannot be an abstract class, because the system immediately creates an internal class object when creating an anonymous internal class. Therefore, anonymous internal classes cannot be defined as abstract classes. 2) The anonymous internal class unequal definition Constructor (constructor, the only class with constructor), because the anonymous internal class has no class name, so the constructor cannot be defined, however, anonymous internal classes can define instance initialization blocks. 3) Anonymous internal classes cannot have access modifiers and static modifiers. */Outter out2 = new Outter () {public void test () {System. out. println ("anonymous internal class method test") ;};/* The say method does not exist in the Outter method, only the following method can be used to call the custom method of the anonymous internal class say () Non-Anonymous internal class object. An internal class without a name in an anonymous internal class cannot use an instance object to call its method. However, the method that inherits the parent class and the implemented method can be called normally. In this example, the anonymous internal class inherits the test method of the class Outter (both the abstract class and the interface can), so it can be called using the Outter object. */New Outter () {public void test () {System. out. println ("anonymous internal class method test");} public void say () {System. out. println ("anonymous internal class method say ");}}. say (); // In fact, an anonymous object is hidden in the anonymous internal class. Using this method, you can directly call the say () and test () methods; out2.test ();}}

The running result is as follows:

Directly access the static member variable of the external class: the static method with a value of 1 of the external class cannot be accessed directly, but can only be accessed through the new external class (). to access non-static members of an external class: the value of B is 5. Non-static methods of external classes static internal class methods ~~~~~~~~~~~~~~~ To access a member with the same name as an external class, you must use the external class (). this. static member variable mode: the value of a is 1. When a hidden phenomenon occurs, the static member variable of the member's internal class is accessed: a is a static method of 20 external classes. To access members of the same name of the external class, you must use the external class (). this. non-static member variable mode: the value of B is 5. The hidden phenomenon accesses non-static member variables of the member's internal class: the value of B is 28 member internal class non-static Method External class non-static method member internal class method ~~~~~~~~~~~~~~~ In the external class method, the value of static member variable a of the external class is 1. In the static method of the external class, the value of non-static member variable B of the external class is 5. non-static methods access local variables in their methods (must be modified using final): The value of c is 5 Local internal class methods ~~~~~~~~~~~~~~~ Anonymous internal class method: say anonymous internal class method test

 

Refer to blog links

Https://www.cnblogs.com/hapjin/p/5744478.html

Https://www.cnblogs.com/dolphin0520/p/3811445.html

 

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.