Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Object.getClass () in Java

Shulou Source: shulou.com Published: 2022-06-02 05:41:03 09月25日 Update

This article mainly introduces "how to use Object.getClass () in Java". In daily operation, I believe many people have doubts about how to use Object.getClass () in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Object.getClass () in Java". Next, please follow the editor to study!

Java Object.getClass () method

Object.getClass () method, the return value of this method is Class type, Class c = obj.getClass (); through object c, we can get all member methods of the object, each member method is a Method object; we can also get all member variables of the object, each member variable is a Field object; similarly, we can also get the object's constructor, which is a Constructor object

For more information, see the following example package classTest; import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method / * print the information of the class, including the constructor and member function of the class The member variable * @ author Wang * * / public class ClassUtil {/ * gets information about the member methods of an object * * @ param obj * / public static void printClassMethodMessage (Object obj) {/ / to get information about a class, you must first get the class type of the class, which subclass's object c is the subclass's class type Class c = class () / / get the name of the class System.out.println ("the name of the class is:" + c.getName ()) / * * Method class, method object A member method is a Method object * the getMethods () method gets all the public functions, including the parent class inherited * getDeclaredMethods () gets all the methods declared by the class itself, regardless of access rights * / / c.getDeclaredMethods () Method [] ms = c.getMethods () For (int I = 0; I

< ms.length; i++) { // 得到方法的返回值类型的类类型 Class returnType = ms[i].getReturnType(); System.out.print(returnType.getName() + " "); // 得到方法的名称 System.out.print(ms[i].getName() + "("); // 获取参数类型--->

What you get is the class type of the parameter list type Class [] paramTypes = Ms [I] .getParameterTypes (); for (Class class1: paramTypes) {System.out.print (class1.getName () + ",");} System.out.println (")") }} / * get the information of the object's member variables * * @ param obj * / public static void printFieldMessage (Object obj) {Class c = obj.getClass () / * * member variables are also objects the java.lang.reflect.Field Field class encapsulates operations on member variables * the getFields () method acquires information about all member variables of public * getDeclaredFields acquires information about member variables declared by the class itself * / / Field [] fs = c.getFields () Field [] fs = c.getDeclaredFields (); for (Field field: fs) {/ / get the class type of the member variable Class fieldType = field.getType (); String typeName = fieldType.getName (); / / get the name of the member variable String fieldName = field.getName () System.out.println (typeName + "" + fieldName);}} / * * print the information of the object's constructor * * @ param obj * / public static void printConMessage (Object obj) {Class c = obj.getClass (); / * * the constructor is also the object java.lang. Constructor encapsulates constructor information * getConstructors gets all public constructors getDeclaredConstructors gets all constructors * / Constructor [] cs = c.getConstructors (); Constructor [] cs = c.getDeclaredConstructors (); for (Constructor constructor: cs) {System.out.print (constructor.getName () + "(")) / / get the parameter list of the constructor-> get the class type of the parameter list Class [] paramTypes = constructor.getParameterTypes (); for (Class class1: paramTypes) {System.out.print (class1.getName () + ",");} System.out.println (")") } Java Object.getClass () source code analysis

1. Stack

Person object reference

2. Heap

Person instance object, Person.class information

Person instance object:

Mark word: object header, which stores instance summary information, such as lock information, hash value, thread information, etc.

Kclass metadata type pointer, pointer to Person type metadata

Instance data: the values of various member variables of the object.

Person.class information

Person.class class information, method, field information.

3. Person type metadata of the old generation.

Mark word: type headers, such as human lock information

Kclass metadata type pointer: type pointer to Person type

Mirror information for the java mirror:Person.class class.

Reflection mechanism

Person object pointer, get the Person object store first

Get the Person type metadata according to the kclass metadata type pointer of the Person object

Get the information of the Person.class class from the java mirror in the Person type metadata

At this point, the study on "how to use Object.getClass () in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Objects information types members methods functions variables data pointers instances learning parameters names subclasses that is pointing more encapsulation help utility Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MySQL Shulou Technology Shulou Information Linux