This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

The difference between class and class

Class is a Java keyword, such as public Class Xxx or Class Xxx, used when declaring Java classes. And Class is a Class. We usually think of classes as abstractions and collections of objects, and Class is the equivalent of abstractions and collections of classes. You can also think of an object as an instance of a Class and a Class as an instance of Class.

Ii. The Class is introduced

Class is a Class. As shown below, it is in the java.lang package.Its constructor is a private property, so we can’t just create a new Class object, as the comment section below says: “Private constructor. Only the Java virtual machine creates class objects. Do not use this constructor and prevent the default constructor from being generated.”

How do I get the Class object

1. Use getClass() to get the Class object

The getClass() method is part of the Object class, as shown below:If we have already created an object of a certain type, we can get the Class object of that type by using the getClass() method:

package Task;
import org.junit.Test;

public class Try0 {
    @Test
    public void toTry(a) throws ClassNotFoundException {
// //forName method: the argument is the path of its class
// Class a = Class.forName("Task.Try1");
// System.out.println(a);

        // Get the class from the object
        Try1 try1 = newTry1(); Class b = try1.getClass(); System.out.println(b); }}class Try1{}Copy the code

Test results:

2. Use the forName() method to get the Class object

The class.forname method is a static method of the Class Class, as shown below:So you can get the Class object directly from class.forname (” Class path “) :

package Task;
import org.junit.Test;

public class Try0 {
    @Test
    public void toTry(a) throws ClassNotFoundException {
        //forName method: the parameter is the path of its class
        Class a = Class.forName("Task.Try1"); System.out.println(a); }}class Try1{}Copy the code

Test run screenshot:

Get class objects (class literal constants)

package Task;
import org.junit.Test;

public class Try0 {
    @Test
    public void toTry(a) throws ClassNotFoundException {
// //forName method: the argument is the path of its class
// Class a = Class.forName("Task.Try1");
// System.out.println(a);

// // gets classes from objects
// Try1 try1 = new Try1();
// Class b = try1.getClass();
// System.out.println(b);

        // Class literal constantsClass c = Try1.class; System.out.println(c); }}class Try1{}Copy the code

Test results:

Class common methods

package Task;
import org.junit.Test;

public class Try0 {
    @Test
    public void toTry(a) throws ClassNotFoundException {
// //forName method: the argument is the path of its class
// Class a = Class.forName("Task.Try1");
// System.out.println(a);

        // Get the class from the object
        Try1 try1 = new Try1();
        Class b = try1.getClass();
        System.out.println(b);

// // class literal constants
// Class c = Try1.class;
// System.out.println(c);


        //isInterface method to check whether it is an interface
        System.out.println(b.isInterface());
        //isArray method to check whether it is an array
        System.out.println(b.isArray());
        //isPrimitive (int isPrimitive and Integer is wrapper
        System.out.println(b.isPrimitive());
        //isAnnotation method, which determines whether it is an annotation. Annotations such as the common Override annotation @override, the unit Test annotation we use
        System.out.println(b.isAnnotation());
        //isInstance (try1, try1, try1, try1, try1, try1, try1)
        System.out.println(b.isInstance(try1));
        //getClassLoader method to get the class loader
        System.out.println(b.getClassLoader());
        //getSuperclass method to get information about the parent class
        System.out.println(b.getSuperclass());
        //getGenericSuperclass method, also get the parent class information
        System.out.println(b.getGenericSuperclass());
        GetComponentType (); //getComponentType ()
        System.out.println(b.getComponentType());
        //getDeclaredClasses to get which class B inherits from
        System.out.println(b.getDeclaredClasses());


        // There are several ways to get name:
        / / getName method
        System.out.println(b.getName());
        / / getTypeName method
        System.out.println(b.getTypeName());
        / / getCanonicalName method
        System.out.println(b.getCanonicalName());
        / / getSimpleName method
        System.out.println(b.getSimpleName());

// Here are a few more methods to list
// getTypeParameters method, get the generic parameter array in the generic class
// the getClasses method gets the inner Class that is public modified in the Class object
GetDeclaredClasses () : getDeclaredClasses ()
// getConstructors method, which gets the constructor of the public modifier
// the getConstructor method finds the corresponding constructor
GetDeclaredConstructors () getDeclaredConstructors ()}}class Try1 {}Copy the code

The output screenshot is as follows:

Java reflection mechanism

Refer to my other blog: Reflection in Java (a primer)