"Java noodles question" 11 What is an inner class? Static Nested class and Inner class are different.

Source: Internet
Author: User
Tags modifier

The Inner class(inner Class) defines the classes in the class. (generally Java's argument)

The Nested class(nested Class) is a static (static) inner class. (generally C + + parlance)
Static inner class: 1 creates an object of the static inner class and does not require an outer class object 2 cannot be from a static An object of an inner class accesses an external class object

The biggest difference between a Java inner class and a C + + nested class is whether it has an external reference.

Inner class : is a class defined inside a class,

A. non-static static members cannot be defined in inner classes (static objects are loaded by default, then static inner classes should be loaded into memory before external classes)

When a static member is in an inner class, the inner class must also be static. static inner classes can only access static members in external classes. There are two scenarios:

1, the inner class is static, but the method is not static. The method needs to be executed by the object invocation.

Outer.Inner in = new Outer.Inner ();//generates a static inner class object.

In.show ();

2, the inner class is static, and the method is static. Static internal classes already exist as objects are loaded, and static methods exist with the loading of internal classes. The object is not needed at this time.

Outer1.Inner.show ();

B. inner classes can directly access member variables in external classes

Non-static inner classes have direct access to members in an external class because the inner class holds an outer class object reference: The external class name. this. Variable name

Static inner classes can directly access static members in external classes, but they actually hold external class names.

C. An inner class can be defined outside the method of an external class, or it can be defined in the method body of an external class , as follows:

The access type of the inner class defined on the outside of the method body can be public,protected, default,private , etc. 4 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 an inner class outside of it, make sure to create an instance object of the outer class first, 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 , but you can use the final or abstract modifier before. This inner class cannot be referenced by other classes that are not visible to other classes, but the instance objects 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 local variable .

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

public class outer{

public void Start () {

New Thread (

New Runable () {

public void Run () {};

}). Start ();

}

}

Static Nested Class , it no longer has an intrinsic class, and in a narrow sense it is not an inner class. There is no difference between the Static Nested class and the normal class's behavior and functionality at run time, but there are some differences in syntax when programming references :

1. It can be defined as public ,protected, default,private , and so on, while ordinary classes can only be defined as public and the default of both types.

2. On the outside, refer to the Static Nested class with the name " external class name . " The internal class name .

3. There is no need to create an instance object of an external class outside , you can create the Static Nested classdirectly, for example, assuming that Inner is defined in the the Static Nestedclass in the Outer class , you can use the following statement to create the Inner class:

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

4. Because the static Nested class does not depend on an instance object of an external class,static Nested class can access the non- static The member variable .

5. When you access static nested class, You can direct use static  Nested class , without the need to add the name of the outer class, in static nested class static member variable , you do not need to add the name of the external class.

The inner class defined in the static method is also static Nestedclass, which isnot allowed to add the static keyword to the class before it, and the statics Nested class is similar to an inner class in a common method, except that it can access static member variables directly in the outer class, but it can also access local variables in the statically method, but before the local variable must be added Final modifier.

Note: The overall characteristics of the inner class: two places can be defined, can access the member variables of the external class, cannot define static members, which is a big feature. And then we'll talk about some details, such as the syntax differences of several definitions, the static inner classes, and the anonymous inner classes.

Transferred from: http://blog.sina.com.cn/s/blog_105817120102vyyv.html

Related information: http://www.cnblogs.com/wangzhongqiu/p/6574019.html

Http://www.cnblogs.com/wangzhongqiu/p/6574110.html

"Java noodles question" 11 What is an inner class? Static Nested class and Inner class are different.

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.