Java internal class and Modifier

Source: Internet
Author: User

Internal classes can be four types of internal static internal class anonymous internal class the similarities of several internal classes: A, internal class is still an independent class, after compilation, internal classes will be compiled into independent ones. class file, but with the class life and $ symbol of the external class. B. Internal classes cannot be accessed in normal mode. An internal class is a member of an external class, so the internal class can freely access the member variables of the external class, whether it is private or not. 2. Internal class of the member: the following code is compiled in the form of class Outer {class inner {}. Two files are generated: outer. Class and outer $ inner. Class. No static declaration is allowed in the member's internal class! The following code cannot be compiled.
Class inner {static int A = 10;} the only way to access the Member's internal class is through the external Class Object! A. instantiate an internal class object from a non-static method of an external class. Class outer {private int I = 10; Public void makeinner () {inner in = new inner (); In. seeouter ();} class inner {public void seeouter () {system. out. print (I) ;}}on the surface, we have instantiated the internal class object without creating an external class object, which is in conflict with the above. In fact, if you do not create an external class object, you cannot call the makeinner () method. Therefore, you must create an external class object in the end. You may try to modify makeinner () method to static method, that is, static
Public void makeinner (). In this way, you can instantiate the external class without creating an external class! But can I access non-static members and methods in a static method? Obviously not. It does not reference this. Failed to jump out of that rule! But what if I instantiate an external class object in this static method and use this object to instantiate the external class? Yes! That is, the next content. B. instantiate an internal class object from a static method of an external class. Class outer {private int I = 10; Class inner {public void seeouter () {system. Out. Print (I );
} Public static void main (string [] ARGs) {outer out = new outer (); outer. inner in = out. new inner (); // outer. inner in = new outer (). new inner (); In. the line commented out by seeouter () ;}} is the merge form of the above two rows, a concise statement. Compare: in non-static external class methods, the internal class object is instantiated in the normal New Method: inner in = new inner (); In the static method of external class, the internal class object is instantiated, you must first create an external Class Object: outer. inner
In = new outer (). New inner (); C. This reference of the internal class. A common class can use this to reference the current object. The same applies to internal classes. But what if the internal class wants to reference the current object of the external class? In the form of "external Class Name". This;, the outer. This is shown in the following example. Class outer {class inner {public void seeouter () {system. out. println (this); system. out. println (outer. this) ;}}d. modifier of the member's internal class. For common classes, available modifiers include final, abstract, strictfp, public, and default package access. However, the internal class of a member is more like a member variable and method. Available modifiers include final, abstract, public, private, protected, strictfp, and static. Once the internal class is modified with static, it becomes a static internal class. 3. Internal class of the method. Put the class in the method as the name suggests.
Class outer {public void dosomething () {class inner {public void seeouter () {}}} A. The internal class of the method can only be instantiated within the method that defines the internal class, it cannot be instantiated outside of this method. B. The class object inside the method cannot use non-final local variables of the method of the internal class. Because the local variables of a method are located on the stack, they only exist in the life cycle of the method. When a method ends, its stack structure is deleted, and local variables become history. However, after the method ends, the internal class objects created in the method may still exist in the heap! For example, if a reference to it is passed to some other code and stored in a member variable. Because the survival of local variables cannot be guaranteed as long as that of class objects in the method, internal class objects cannot use them. The following is a complete example:
Class outer {public void dosomething () {final int A = 10; Class inner {public void seeouter () {system. out. println (a) ;}} inner in = new inner (); In. seeouter ();} public static void main (string [] ARGs) {outer out = new outer (); out. dosomething () ;}} C. modifier of the internal class of the method. Unlike the internal class of a member, the internal class of a method is more like a local variable. Only final and abstract classes can be used to modify methods. D. Internal class of the method in the static method. The static method does not reference this. Therefore, the internal class in the static method suffers the same treatment, that is, it can only access the static members of the external class. 4. Anonymous internal class. As the name suggests, there is no name for the internal class. On the surface, they seem to have names, but they are not actually their names. A. inherited anonymous internal class.
Class car {public void drive () {system. Out. println ("driving a car! ") ;}} Class test {public static void main (string [] ARGs) {car = new car () {public void drive () {system. out. println ("driving another car! ") ;}}; Car. Drive () ;}} output: driving another
Car! The car reference variable is not a reference car object, but an object of the car anonymous subclass. The key to building an anonymous internal class is to override one or more methods of the parent class. It is emphasized that the method of rewriting the parent class, rather than creating a new method. Because it is impossible to call methods that the parent class does not have by referencing the parent class! Creating a new method is unnecessary. In short, refer to polymorphism. B. Interface-Based Anonymous internal class. Interface vehicle {public void drive ();} class test {public static void main (string [] ARGs) {vehicle v = new vehicle (){
Public void drive () {system. Out. println ("driving a car! ") ;}}; V. Drive () ;}} the code above is strange and seems to be instantiating an interface. This is not the case. An interface-Based Anonymous internal class implements an interface-Based Anonymous class. Only one interface can be implemented. C. Parameter-Based Anonymous internal class. Class bar {void dostuff (FOO f) {}} interface Foo {void Foo ();} class test {static void go () {Bar B = new bar ();
B. dostuff (New Foo () {public void Foo () {system. Out. println ("foofy") ;}}} 5. Static nested class. Technically, static Nested classes do not belong to internal classes. Because the internal class shares a special relationship with the external class, it is more specifically the sharing relationship with the instance. Static Nested classes do not have the above relationship. It is only located inside another class, so it is also called a top-level nested class. Static means that the internal class can also be accessed when there is no external class object, just like other static members. Static Nested classes cannot access members and methods of external classes. Class outer {static class
Inner {}} class test {public static void main (string [] ARGs) {outer. inner n = new outer. inner () ;}} B. The internal Class Object of the method cannot use non-final local variables of the method of the internal class. Because the static nested class does not have the above relationship. It is only located inside another class, so it is also called...

Related Article

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.