Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

java.lang.Class class


May 10, 2021 Java



An instance object of a Class class that records class description information.

Class classes do not have a common construction method and cannot be instantiated by new operators; t o get an instance.

Method Objective
static ClassforName(String className)throws ClassNotFoundException Use the parameter className to specify a specific class to get the relevant class description object, which is likely to throw a ClassNotFoundException that must be snapped
Class getSuperclass() Gets the description object of the parent class of the current class description object
String getName() Returns the class name of the object described by the current class

There are three ways to get Class objects:

public class _T11 {
	// Class:类描述对象
	public static void main(String[] args) {
		Class<?> _class;
		// ***1*对象.getClass()
		String str = "";
		_class = str.getClass();
		System.out.println(_class + "-----对象名.getClass()");
		// ***2*类.class
		_class = String.class;
		System.out.println(_class + "-----类名.class");
		// ***3*Class.forName("")
		try {
			_class = Class.forName("java.lang.String");
			System.out.println(_class + "-----Class.forName(...)");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
class java.lang.string----- object name.getClass()
Class java.lang.String ----- class name .class
class java.lang.String-----Class.forName(...)

Common methods for Class classes:

  • getName()
    A Class object describes the properties of a particular class, and getName, the most common method in the Class class, returns the name of the entity (class, interface, array class, base type, or void) represented by this Class object as String.
  • newInstance()
    Class also has a useful way to create an instance for a class called newInstance(). F or example: x.getClass.newInstance(), create a new instance of the same type as x. The newInstance() method calls the default constructor (no parameter constructor) to initialize the new object.
  • getClassLoader()
    Returns the class loader for the class.
  • getComponentType()
    Returns Class, which represents the type of array component.
  • getSuperclass()
    Returns a class that represents a superseed of entities (classes, interfaces, basic types, or voids) represented by this Class.
  • isArray()
    Determines whether this Class object represents an array class.