Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A detailed introduction to active and passive references to classes in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the detailed introduction of active and passive references to classes in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the detailed introduction of active and passive references to classes in Java.

Active reference. Here are five scenarios of active reference.

1. When you encounter the four bytecode instructions of new,getstatic,putstatic,invokestatic, the class will be initialized if it is not initialized, create objects, read or set static fields, and call static methods.

2. Reflection

3. The parent class is initialized before the subclass is initialized.

4. The class that contains the main method will be initialized when the virtual machine starts.

5. When using jdk's dynamic language support (unknown)

Passive reference:

Class SuperClass {static {syso ("super init");} public static int value=123;} class SubClass extends SuperClass {static {syso ("sub init")}} public class ConstantClass {public static final HW= "helloworld";} public class NotInitialization {main () {syso (SubClass.value) / / SuperClass sc [] = new SuperClass [10] / / syso (ConstantClass.HW);}}

Output: super init 123. for access to static fields, only the class that defines the field directly will be initialized, the subclass will not initialize, the first comment will be canceled and run without any output, and the reference class through the array definition will not trigger the initialization of the class, but will initialize an initialization of a subclass automatically generated by the virtual machine that inherits from the object class, which represents the array The properties and methods of the array are implemented in this class (length attribute and clone ()), there will be no output after the second comment is cancelled, constants will be put into the constant pool during compilation, ConstantClass.HW will be put into the constant pool of the NotInitialization class during compilation, and NotInitialization will not have a symbolic reference to NotInitialization.

At this point, I believe you have a deeper understanding of "the detailed introduction of active and passive references to classes in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report