Class object

Class objects are used to describe bytecode files (i.e., Class files)

Classes, interfaces, enumerations… Such as these

In Java, Class itself corresponds to a Class, which represents a Class file. Any Class file is an object of Class.

Three ways to get class objects

① Call directly with the class name

You can think of it as a static property of the class, so you can call it directly with the class name.

It’s not accurate, of course, but it’s a way of thinking about memory.

The forName method of the Class

The Class Class has a static method forName() that can be called directly using the Class name Class;

The argument is the fully qualified name of the corresponding class, i.e., package name + class name.

③ Call getClass for the object name

Every object has a corresponding class object.

This can be obtained using the getClass method, where the array also has a corresponding class object.

2 Predefined Objects

Pre-defined basically means void and basic data types, which are already defined in Java.

Just like when we buy a new mobile phone, we also come with various system software.

The keyword void also has a corresponding Class object

Void represents no return value type, so it is also a data type, meaning no type, so the output object is void itself.

The Class voidClass = void. The Class; The object voidClass holds void.

② Basic data types

Boolean, byte, char, short, int, long, float, double

Basic data types are kept in memory, so they also have their own bytecode.

The Class objects obtained are themselves stored, because the base data types do not belong to any package.

For example, Class c = int. Class, c holds an int.

To sum up: every data type has its own Class object

Second, the reflection

Reflection is a mechanism that allows classes to be dissected during program execution (primarily in frameworks).

What’s in a class?

Constructor, member method, member object, let’s go through them.

Constructor object

The normal way to create an object, which we use a lot, is to simply create a new object.

We are going to use reflection to create objects:

① Get the Class object

SClass is the Class object of the Student Class.

② Get the constructor

Constructor, a class in Java.

SClass has a method called getConstructor(), which, as its name implies, gets a constructor.

No parameters for empty parameter construction.

③ The constructor creates the object

Instance is translated as an instance, that is to say, newInstance is the instantiation of a class, and an object is the instantiation of a class.

So here’s the question:

I can create an object with one line of code, but with reflection it takes three lines of code.

To be honest, I think so too, but reflection is generally used in frameworks, not in real development, but since it is used in frameworks, there must be something cool about it.

Here’s a simple special:

When the constructor is private, there is no way to directly new the object, but reflection can:

① Get all declared constructs

Methods Declared can be obtained by any Class object on which a Class is Declared.

  • GetConstructor: Gets only constructs that are public modified.
  • GetDeclaredConstructor: Gets all declared constructs.

I can get it now, but I still get an error if I print it directly. Why?

It is as if a private constructor locks a constructor;

If you create an object the normal way, even this constructor won’t be available.

GetDeclaredConstructor = getDeclaredConstructor = getDeclaredConstructor;

② Modifying permission

The constructor has a method called setAccessible(), Accessible, which translates to reachable, and when the argument is true, it’s reachable.

It’s easy to understand. It’s like unlocking the lock.

2 Method Object

1) getMethod () method

As the name implies, this is a method that gets a member method of a class.

This method takes two parameters:

  • String name
  • Class
    … parameterTypes

The first parameter is the method name and the second parameter is the parameter type.

That is, based on the method name and parameter type, you can find the corresponding member method in the class.

(2) the invoke method

‘Invoke’ translates to ‘call’.

This method also takes two parameters:

  • Object Object: The Object on which the method is invoked
  • The Object… Args: Arguments to which this object calls a method

The first argument is who called the method, and the second argument is the method argument.

Show.invoke (stu.” Liu Xiaoai “) is equivalent to:” Liu Xiaoai “)

In the same way, reflection can access private methods:

1) getDeclaredMethod method

  • GetMethod: Methods that can only get public decorations.
  • GetDeclaredMethod: The method that can get all declarations.

The setAccessible() method is then called to set the permission to true, allowing access to the private method.

3 Property Object

1) getField method

As the name implies, gets the property, and the parameter is the property name.

That is, based on the name of the attribute in the class, we can find the corresponding member variable in the class.

(2) set method

Set the property method, which takes two arguments:

  • Object obj: The Object associated with the property
  • Object value: parameter

Name. Set (stu,” liu Xiaoai “), equivalent to stu.setName(value)

Set the name attribute in stU object to “Liu Xiaoai”

(3) the get method

Gets the name attribute in the STU object.

In the same way, reflection can acquire private properties

① Access private attributes

Same thing

  • GetField: Can only get properties that are public qualified.
  • GetDeclaredField: Retrieves all declared properties.

The setAccessible() method is then called to set the permission to true to access the private property.


Thanks for watching.

If you can, please give it a thumbs up. Thank you.