Java internal classes (anonymous classes, anonymous objects, static internal classes) details and examples

Introduction to internal classes

A class defined in another class is called an inner class

Member inner class

1.. New to create an inner class of a member, you must first create an instance of the outer class, and then pass New creates an object of the inner class

2.. This can be through the class name of the external class This to access all properties and methods of the external class.

Anonymous Inner Class

For a class without a name, an object will be created at the same time as the class is created.

Anonymous inner classes can be used for classes that need to be used only once

Anonymous object

The object only needs to be accessed once

Static inner class

1. Static internal classes can only access static methods and variables of external classes, not non static ones.

2. Static internal classes can be created directly without creating references to external classes.

Anonymous inner classes access local variables

Internal class access to local variables must be final. If not, jdk1 8 added by default. When the variables used are changing, you can use the following method, or you can set the following I as static at the beginning

The following describes the implementation skills of internal classes

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>