Java.lang.Class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.