Java like objects and class loaders

Personal homepage:Golden Scales Stepping on the Rain

Personal introduction: Hello everyone, I amJinlin, a fledgling Java novice< /span>

Current situation: A 22nd ordinary undergraduate graduate. After many twists and turns, he now works for a large and well-known domestic daily chemical company, engaged in Java development

My blog: This is CSDN, where I learn technology and summarize knowledge. I hope to communicate with you and make progress together ~

class object

Java's Class Object is an object that represents the metadata of a class at runtime.

Each class has a corresponding class object at runtime.

This class object contains information about the class, such as class methods, fields, constructors, annotations, inheritance relationships, etc.

Through class objects, you can analyze and manipulate the structure of the class at runtime. This is the basis of Java's reflection mechanism.

class loader

Java Class Loader (Class Loader) is part of the Java Virtual Machine (JVM) and is responsible for loading the bytecode of classes from different sources into memory , and generate the corresponding class object at runtime.

The class loader is an important part of the Java runtime environment. It allows Java applications todynamically load classes, thereby achieving flexible Class loading mechanism.

What is the relationship between class objects and class loaders?

Class Loader (ClassLoader) and Class Object (Class) are two different, but related concepts in Java.

the difference

  • Function: The class loader is used to load the bytecode of the class into memory, while the class object is used to obtain class information and perform reflection operations at runtime.
  • On the object type: A class loader is a class in Java that implements the java.lang.ClassLoader interface or its subclasses. A class object is an object that represents the metadata of a class, represented by an instance of the java.lang.Class class. Loading process: The class loader loads the bytecode of a class from the file system, network, or other sources based on its name. The class object is generated after the class loader loads the bytecode of the class, and it contains class information.

After class loader loads the bytecode of the class, it will generate the correspondingclass object, class Loaders and class objects are closely related, but they represent different concepts.

In general,A class loader is a mechanism for loading classes, while a class object is a tool for manipulating the metadata of a class and performing reflection . They play different but interrelated roles in Java's runtime environment.

How many ways are there to obtain class objects in Java?

In Java, you can usea variety of ways to obtain class objects (Class objects)

1. Get the class object through the class name

Class objects can be obtained directly using the fully qualified name of the class (including the package name). This approach is useful if you already know the class name.

// MyClass是你要获取的类名
Class<?> myClass = MyClass.class;

2. Obtain the class object through the getClass() method of the object

If you have an instance of a class, you can get the class object by calling the instance's getClass() method.

Object obj = new MyClass();
Class<?> myClass = obj.getClass();

3. Use the Class.forName() method

Class.forName() method Loads and returns a class object from the class path by providing the fully qualified name of the class

try {
    Class<?> myClass = Class.forName("com.example.MyClass");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

4. Obtain class objects through class loaders

You can also use the class loader's loadClass() method to obtain a class object. This is useful for loading classes using a specific classloader.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
    Class<?> myClass = classLoader.loadClass("com.example.MyClass");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

The above method may throwClassNotFoundException exception if the specified class cannot be found. 

How many ways does Java obtain a class loader?

In Java, you can obtain the class loader using different methods. Generally speaking, there are three main ways to obtain a class loader:

1. Get the class loader through the class

In Java, every class has a class loader associated with it. You can get the class loader through the getClassLoader() method.

ClassLoader classLoader = MyClass.class.getClassLoader(); 

2. Get the class loader through the current thread

Each thread has an associated context class loader in Java.

You can use Thread.currentThread().getContextClassLoader() to get the context class loader of the current thread.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

3. Obtain the class loader through the system class loader

There is a default class loader in Java, called the system class loader (also called the application class loader), which is responsible for loading classes on the application class path. You can use ClassLoader.getSystemClassLoader() to get the system class loader.

ClassLoader classLoader = ClassLoader.getSystemClassLoader();

It should be noted that the class loaders obtained by these methods may not be the same instance, because in Java class loaders can form a hierarchy, and each class loader has a parent class loader.

In different contexts, different class loaders may be used.

The article ends here. If you have any questions, you can point them out in the comment area~

I hope to work hard with the big guys and see you all at the top

Thank you again for your support, friends! ! !

Guess you like

Origin blog.csdn.net/weixin_43715214/article/details/132410571