Java-nested class, java-nested

Source: Internet
Author: User

Java-nested class, java-nested

Java Allows defining another class in a class, which is called class nesting. There are two types of class nesting: static class, non-static class, and internal class.

Reasons for using nested classes:

The nested class also belongs to the class member. Therefore, you can use the class member's visible range control modifier. The internal class can use other class members of its class, static Nested classes cannot use other class members of their classes.

Static nesting class

Similar to static fields, static Nested classes are related to their classes. Static Nested classes cannot directly use instance variables or instance fields, but can only be referenced by one object. Static Nested classes can be considered as the same as other top-level classes, but embedded in other classes, convenient packaging.

The usage of static Nested classes is similar to that of other class members in the class. The following shows how to create static nested class objects:

// A nested class of StaticNestedClass OuterClass. StaticNestedClass nestedObject = new OuterClass. StaticNestedClass ();

Internal class (non-static nested class)

Internal classes are related to the instances of their classes. They can directly use the methods and fields of instance objects. Internal classes are related to instances. Therefore, internal classes cannot define static members.

To create an internal class object, you must first create the Class Object of the internal class, as shown below:

// 1 create the object of the class where the internal class is located OuterClass outerObject = new OuterClass (); // 2 create the internal Class Object
// Note the differences between the methods used by constructors of static Nested classes OuterClass. InnerClass innerObject = outerObject. new InnerClass ();

Masking of Nested classes

When we declare a type, if its name is the same as the declaration of another type in the code block where the current code block (such as a method) is located (such as inside the class, this phenomenon is called masking. To use a masked type, we cannot directly reference its name, as shown in the following example:

Public class ShadowTest {public int x = 0; // the following statement of the nested class FirstLevel {// masks the public int x = 1 field of the class whose name is x; // The Declaration of the following method will obscure the void methodInFirstLevel (int x) {System. out. println ("x =" + x); System. out. println ("this. x = "+ this. x); System. out. println ("ShadowTest. this. x = "+ ShadowTest. this. x); // note the usage of this keyword} public static void main (String... args) {ShadowTest st = new ShadowTest (); ShadowTest. firstLevel fl = st. new FirstLevel (); fl. methodInFirstLevel (23 );}}

The above code is output:

X = 23
This. x = 1
ShadowTest. this. x = 0

Serialization. In this tutorial, we strongly recommend that you do not serialize internal classes.

In addition to non-static nesting, there are two internal classes: local and anonymous.

Local class

Local classes can be defined in any code block (in curly brackets) and are generally used in methods.

Local classes can use class members of their top-level classes. In addition, local classes can also use local variables. However, the local classes they use must be modified with the final keyword, that is, immutable variables. In java SE8, local classes can use local variables that are essentially unchanged. That is, even if the local variables are not modified by the final keyword, their values have never changed since the initialization.

Starting from Java 8, local classes can also use the parameters of their methods.

Similar to internal classes, local classes cannot define static members, and local classes defined in static methods cannot use instance members.

APIS cannot be defined in code blocks because they are static in nature. You cannot define an excuse member in a local class. However, constant variables can be defined in a local class (modified using final, whose type is basic data type or string, and initialized during compilation ).

Anonymous class

An anonymous class can be code that is more concise, without a name. It can be declared and instantiated step by step.

The declaration of an anonymous class is an expression, just like calling a constructor. The difference is that it keeps up with the code block of a defined class.

The expression defined by the Anonymous class includes the following parts:

  • New keywords
  • An excuse for implementing this anonymous class or the name of the inherited parent class
  • A pair of parentheses that contain parameters. When an interface is implemented, the parameter is left blank.
  • Anonymous class subject. Similar to the class subject, you can define a method.

For anonymous classes, the available types are the same as for local classes:

  • You can use the class members of the class
  • You can use a local variable with a final modifier in the code block where it is located, or a local variable that is not assigned a value after initialization (java8)
  • For masking types, you cannot directly reference them by name.

Similarly, an anonymous class cannot declare static members or interfaces, but can declare constant variables. In an anonymous class, you can declare instance fields, instance methods, instance initialization code blocks, and local classes.

 

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.