Constructor <User> constructor = User.class.getConstructor(new Class[]{String.class,String.class,String.class}); System.out.println("Constructor "+constructor); constructor.newInstance("Hello","From","Reflection"); 

From this example, it is clear to me that when a constructor is passed to it, an Object of an array of type Class .

But it is misleading: Class.class then allowed to call the Reflection API methods. What does this .class mean?

I know that to access the methods I need to create an object of any class, or if the method is static, then access will be made through the class name .. In the case of access to the Reflection API, this is the end of .class, I can call .class on any class or interface or primitive int.class as a JVM (if it is right to say so) refers to this kind of constructions? What is it and why is it used?

    1 answer 1

    A call to .class after the type name returns an object of type Class . This object contains information about the type (whether it is a class or interface) and is used in those places where you need to transfer information about the type (reflection as one of the examples).

    Calling .class on a type is equivalent to calling getClass() on a type instance . Usually .class used where you need to specify the type, but there is no specific instance of this type. Example for String :

     System.out.println(String.class.getName()); System.out.println("hello".getClass().getName()); 

    In your example, in the first line you find the constructor of the class User , which takes three string parameters. To specify the types of parameters, use String.class .