What is an internal class? Static Nested Class and Inner Class are different ., Nestedinner

Source: Internet
Author: User

What is an internal class? Static Nested Class and Inner Class are different ., Nestedinner

What is an internal class?Static Nested ClassAndInner Class.
An internal class is a class defined inside a class. A static member cannot be defined in an internal class.
Sex, just to find a place, so you need to put it in a class. You need to put it in such a small matter.
It's too much for a class inside the class! Provides internal classes, not to let you do such a thing, boring, not to let you do it.
I think it is possible that since static members are similar to global variables in c language, internal classes are usually used to create internal objects,
Therefore, putting "global variables" in the internal class is meaningless. Since it is meaningless
The internal class can directly access the member variables in the external class. The internal class can be defined outside the external class's method,
You can also define the method bodies of external classes as follows:

Public class Outer {int out_x = 0; public void method () {Inner1 inner1 = new Inner1 (); public class Inner2 // internal class defined inside the method body {public method () {out_x = 3 ;}} Inner2 inner2 = new Inner2 ();} public class Inner1 // internal class defined outside the method body {}}

 


The access types of internal classes defined outside the method body can be public, protecte, default, private, and so on,
This is like the four access types of the member variables defined in the class. They determine that the definition of this internal class is
No visible; in this case, we can also create instance objects of internal classes and instance objects of internal classes.
You must first create an external class instance object, and then use this external class instance object to create an internal class instance object
The Code is as follows:

Outer outer = new Outer();Outer.Inner1 inner1 = outer.new Innner1();

 


There is no access type modifier before the internal class defined in the method, just like the local variable defined in the method,
However, the final or abstract modifier can be used before the internal class. This internal class is invisible to other classes.
Its class cannot reference this internal class, but the instance objects created by this internal class can be passed to other classes for access. This internal
The class must be defined first and then used. That is, the internal class definition code must appear before the class is used.
The same applies to local variables that must be defined first. This internal class can access local variables in the method body,
However, the final modifier must be added before the local variable.
For these details, you only need to try to write code in eclipse, according to the various error messages prompted by the development tool, you can immediately
Understand.
You can also use the following syntax inside the method body to create an anonymous internal class, that is, to define the same
You do not need to define a name for this subclass:

public class Outer{public void start(){new Thread(new Runable(){public void run(){};}).start();}}

 


Finally, you can add the static keyword before the internal Class defined externally by the method to become the Static Nested Class,
It no longer has the characteristics of internal classes. In a narrow sense, it is not an internal class. Static Nested Class and common
There is no difference between the behavior and function of a class at runtime, but there are some differences in the syntax of programming references. It can
It can be defined as public, protected, default, private, and other types, while common classes can only be defined as public and
The two types. The name of the Static Nested Class referenced outside is "external Class name. Internal Class name ". Outside
You can directly create a Static Nested Class without creating an instance object for an external Class. For example, assume that
Is the Static Nested Class defined in the Outer Class, you can use the following statement to create an Inner Class:

Outer.Inner inner = newOuter.Inner();

 


Because the static Nested Class does not depend on the Instance Object of the external Class, the static Nested Class can access
Non-static member variables of the external class (you cannot directly access them. You must create an external class instance to access non-static variables ). When
When accessing the Static Nested Class in the external Class, you can directly use the name of the Static Nested Class
You need to add the external Class name. In the Static Nested Class, you can also directly reference the static member of the external Class.
Variable. No external class name is required.
The internal Class defined in the Static method is also static Nested Class. In this case, the Static keyword cannot be added before the Class,
The Static Nested Class in the Static method is similar to the internal Class application method in the common method.
You can directly access static member variables in an external class and local variables in a static method. However
The final modifier must be added before the variable.
 

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.