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

Detailed explanation of Class Class and Object Class in Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains the "detailed explanation of Class class and Object class in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the detailed explanation of Class class and Object class in Java".

Class Class and its usage in Java

When the Java program is running, the Java runtime system always carries out the so-called runtime type identification on all objects, that is, the so-called RTTI.

This information records the class to which each object belongs. Virtual machines usually use run-time type information to select the correct method to execute, and the class used to hold these types of information is the Class class. The Class class encapsulates the runtime state of an object and interface, and objects of type Class are created automatically when the class is loaded.

To put it bluntly:

The Class class is also a class, except that the name is very similar to the class keyword. Java is a case sensitive language.

The object content of the Class class is the type information of the class you created. For example, if you create a shapes class, Java will generate an object whose content is the Class class of shapes.

The object of the Class class cannot be created as new shapes () like an ordinary class, its object can only be created by JVM, because this class does not have a public constructor

/ * Private constructor. Only the Java Virtual Machine creates Class objects. * This constructor is not used and prevents the default constructor being * generated. * / Private constructor, which can only be instantiated by jvm private Class (ClassLoader loader) {/ / Initialize final field for classLoader. The initialization value of non-null / / prevents future JIT optimizations from assuming this final field is null. ClassLoader = loader;}

The purpose of the Class class is to provide or obtain type information for an object at run time, similar to the typeid () function in C++. This information can also be used for reflection.

Class class principle

Take a look at some of the source code of the Class class

/ / various types of information are encapsulated in the Class class. In jvm, all the information about each Java class is obtained through an instance of the Class class. Public class Class class {Class aClass = null;// private EnclosingMethodInfo getEnclosingMethodInfo () {/ / Object [] enclosingInfo = getEnclosingMethod0 (); / / if (enclosingInfo = = null) / / return null;// else {/ / return new EnclosingMethodInfo (enclosingInfo); / /} / /} / * * provides atomic operation * Atomic operations support. * / private static class Atomic {/ initialize Unsafe machinery here, since we need to call Class.class instance method// and have to avoid calling it in the static initializer of the Class class...// private static final Unsafe unsafe = Unsafe.getUnsafe (); / offset of Class.reflectionData instance field// private static final long reflectionDataOffset;// offset of Class.annotationType instance field// private static final long annotationTypeOffset / offset of Class.annotationData instance field// private static final long annotationDataOffset;//// static {/ / Field [] fields = Class.class.getDeclaredFields0 (false); / / bypass caches// reflectionDataOffset = objectFieldOffset (fields, "reflectionData"); / / annotationTypeOffset = objectFieldOffset (fields, "annotationType"); / / annotationDataOffset = objectFieldOffset (fields, "annotationData") / / provide reflection information / / reflection data that might get invalidated when JVM TI RedefineClasses () is called// private static class ReflectionData {/ / volatile Field [] declaredFields;// volatile Field [] publicFields;// volatile Method [] declaredMethods;// volatile Method [] publicMethods;// volatile Constructor [] declaredConstructors;// volatile Constructor [] publicConstructors / Intermediate results for getFields and getMethods// volatile Field [] declaredPublicFields;// volatile Method [] declaredPublicMethods;// volatile Class [] interfaces;//// Value of classRedefinedCount when we created this ReflectionData instance// final int redefinedCount;//// ReflectionData (int redefinedCount) {/ / this.redefinedCount = redefinedCount / / method array / / static class MethodArray {/ Don't add or remove methods except by add () or remove () calls.// private Method [] methods;// private int length;// private int defaults;//// MethodArray () {/ / this (20) / /} / MethodArray (int initialSize) {/ / if (initialSize < 2) / / throw new IllegalArgumentException ("Size should be 2 or more"); / methods = new Method [initialSize]; / / length = 0 initialSize / defaults = 0 / /} / / Note Information / / annotation data that might get invalidated when JVM TI RedefineClasses () is called// private static class AnnotationData {/ / final Map

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