Java.lang.Class
First, what is class?
Class is a category that is located under the Java.lang package.
In Java, each class has an object of the corresponding class class, in other words: When a Java program starts running After a Xxx.java class has been compiled to generate the Xxx.class file, an object of class class corresponding to the XXX class is generated in the JVM virtual machine to represent the type information of the XXX class.
Second, class common methods
class is the core class in reflection, and it has the following methods:
Get Properties in class:
-GetFields (): Gets the genus of the public type in the class
-GetField (String name): Gets the class-specific method that specifies the name of the property
-Getdeclaredfields (): Gets all the properties in the class (public, protected, Default, private), but does not include inherited properties.
-Getdeclaredfield (String name): Gets the class-specific method, The name parameter specifies the names of the properties
get constructors in class:
-GetConstructors (): Gets the public method in the class
-GetConstructor ( Class[] Params: Gets the specific constructor method of the class, the params parameter specifies the parameter type of the constructor method
- Getdeclaredconstructors (): Gets all the constructor methods in the class (public, protected, default, private)
Getdeclaredconstructor (class[] params): Gets the specific constructor method of the class, the params parameter specifies the parameter type of the constructor method
Get methods in class:
-GetMethod (String na Me, class[] params): Gets the specific method of the class, the name parameter specifies the method's moniker, and the params parameter specifies the method's parameter type
-Getdeclaredmethods (): Gets all the methods in the class (public, protected, default, Private)
-Getdeclaredmethod (String name, class[] params) : Gets the specific method of the class, the name parameter specifies the method's first names, and the params parameter specifies the method's parameter type
Other important methods:
-Newinstance (): Creates an object of this class by constructing a class without parameters
-forname (S Tring ClassName): Gets the class object that is specified by the ClassName parameter
-forname ( String Classname,boolean Initialize,classloader): Gets the class object that is specified by the ClassName parameter using the specified class loader
-getClassLoader (): Gets the ClassLoader
-GetName (): Gets the class name
-Getpackage (): Gets the package name where the class resides
Iii. three ways to get class objects
First: Call the class's static method forname, such as Class.forName ("java.lang.String")
The second type: Use the class's. class syntax, for example: Class CLS = String.class
The Third Kind: Call the object's GetClass method, for example: String str = "123"; Class cls = Str.getclass ();
(PS: The GetClass () method is defined in the Java.lang.Object class, so the type of the object can be obtained by this method for any Java object)
Java.lang.Class