What is an internal class

Source: Internet
Author: User

An inner class is a class defined inside a class that cannot define a static member ( static member is not an object's attribute, just to find a place to go, so it needs to be put into a class, so small, you have to put it in a class inside the class, That's too much! Provide inner class, not for letting you do this kind of thing, boring, not letting you do it. I think it might be that since static members are similar to C-language global variables, and inner classes are often used to create internal objects, it is meaningless to put "global variables" in internal classes, since they are meaningless things that should be banned. Inner classes can directly access member variables in external classes, which can be defined outside the method of an external class, or in the method body of an external class, as follows:

public class Outer

{

int out_x = 0;

public void Method ()
{

Inner1inner1 = new Inner1 ();

Publicclass Inner2//inner class defined inside the method body

{

Publicmethod ()

{

Out_x= 3;

}

}

Inner2inner2 = new Inner2 ();

}

Publicclass Inner1//inner class defined outside the method body

{

}

}

The access type of an inner class defined outside the method body can be public,protecte, default, private, and so on 4 types, as if the member variable defined in the class has 4 access types. They determine whether the definition of this inner class is visible to other classes, and in this case we can create an instance object of the inner class outside, create an instance object of the inner class, create an instance object of the outer class, and then use the instance object of the outer class to create an instance object of the inner class. The code is as follows:

Outer Outer = new Outer ();

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

You cannot have an access type modifier before an inner class defined inside a method, just as a local variable is defined in a method, but you can use the final or abstract modifier in front of such inner classes. This inner class is invisible to other classes and cannot reference this inner class, but an instance object created by this inner class can be passed to other classes for access. This inner class must be defined first, and then used, that is, the definition code of the inner class must appear before the class is used, which is the same as if the local variable in the method must be defined before it is used. This inner class can access local variables in the method body, but must have a final modifier before the office variable.

For these details, as long as you try to write code in Eclipse, you can get a quick look at the various types of error messages that are developed by the ToolTip.

Inside the method body, you can also create an anonymous inner class that defines a subclass of an interface or class, and also creates an instance object of the subclass, without defining a name for the subclass:

public class Outer

{

Publicvoid Start ()

{

Newthread (

New Runable () {

Publicvoid run () {};

}

). Start ();

}

}

Finally, the inner class defined outside the method can be preceded by the static keyword, which becomes the static Nestedclass, which no longer has an intrinsic class , and all, narrowly speaking, is not an inner class . There is no difference between Static Nested class and normal class at run time, but there are some differences in syntax when programming reference, it can be defined as public, protected, default, private, etc. The normal class can only be defined as public and the default two types. Outside the name of the static Nested class class is "external class name. Internal class name". You can create the static Nested class directly outside without creating an instance object of the external class , for example, assuming that inner is a static Nested class defined in the outer class, you can create a inner class using the following statement:

Outer.Inner Inner = new Outer.Inner ();

Because the static Nested class does not depend on the instance object of the external class, the static Nested class can access the non-static member variables of the outer class. When you access the static Nested class in the external class, you can directly use the name of the static Nested class without adding the name of the outer class, and in the static Nested class you can refer directly to the static member variables of the outer classes. You do not need to add the name of an external class.

The inner class defined in the static method is also static Nested class, at this time cannot add the static keyword in front of the class, static Nested class is similar to how the inner class in the normal method is applied In addition to the direct access to static member variables in the outer class, it is possible to access local variables in the statically method, but the local variable must be added to the final modifier before it.

Note: The overall characteristics of the inner class: for example, in two places can be defined, can access the external class member variable, cannot define static member, this is a big feature . There are several syntactic differences in how they are defined, static inner classes, and anonymous inner classes.

What is an internal 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.