Java Internal classes

Source: Internet
Author: User


Although the use of internal classes violates Java's simpler design concept than C + +, the so-called existence is reasonable, inner classes can be hidden from other classes in the same package, and internal class methods can access the data in the scope of the class definition, including private data. The solution to using anonymous inner classes in some cases is shorter and easier to understand.

An inner class is a class that is defined in another class. Can be defined either in a class or in a method of a class.

An object of an inner class has an implicit reference that refers to an external class object that instantiates the inner object, so that through this pointer you can access the entire state of the outer class, which can access both its own data domain and the data field of the external class object that created it. Note: The static inner class does not have this additional pointer.

The following describes the local inner class static inner classes and anonymous inner classes, respectively. Before the introduction, simply say the static and final keywords in java.

Final keyword

The modified property indicates that the property value cannot be changed after initialization;

Modification method, which means that the method is not writable, and the compiler encounters the embedded mechanism when the final method is called, which greatly improves the execution efficiency;

A decorated class that indicates that a class cannot be inherited and has no subclasses.

The final keyword has the meaning of being unable to change the end state, can modify the local variable, the instance variable, the static variable, after creation, can only assign the value once, but the definition does not have to initialize. (The final attribute in the interface must be initialized) also modifies the parameter, which represents the read argument, but cannot change the value of the parameter.

Static keyword

Decorated properties, which represent public properties, all instance objects shared, also called Class properties, static properties, the JVM allocates memory only once for static variables, and in memory no matter how many instances have only one copy.

The adornment method, which represents a class method, is directly accessible directly from the class name, as is the Class property.

Modifies the inner class and cannot be decorated with ordinary classes.

Static blocks of code that execute when the JVM loads the class, and if there are multiple, execute sequentially in the order in which each code block is executed once.

Local inner class

Classes implemented in the methods of external classes are completely hidden from the outside world, and there is no other way to know the existence of the class, except to change the method. Local classes cannot be declared with public and private access specifiers. Its scope is scoped to the block of the local class. In contrast to other inner classes, a local inner class can access not only the state of its outer class, but also local variables in the method that defines it, but must be a local variable of the final type.

Anonymous inner class

An inner class can also inherit a class or implement an interface, and if only one object of that class is created, it does not have to be named. The constructor has the same name as the class name, there is no class name for the anonymous class, so there is no constructor, instead, the constructor argument is passed to the parent class constructor, and if the inner class implements the interface, there cannot be any constructors.

Static Inner class

Sometimes an inner class is used only to hide a class inside another class and does not require an inner class to refer to an external class object. To do this, you can declare the inner class to be static in order to cancel the resulting reference. At this point, the inner class cannot access the state of the outer class.


Java Internal 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.