I have come across code in the form:
MyClass.class.getName();
Is .class a property?
Is it applicable to ALL classes? (is it inherited from Object class?)
What type of object is returned by .class ?
What other functions like getName() does the .class have?
I realize that this is a very basic question, but I wasn't able to locate comprehensive information in the Javadocs, and it would be really helpful if some practical application of .class could be given.
Thanks in advance.
MyClass.class is a literal value of type Class (the same way as "..." is a literal value of type String).
It's available for all classes and interfaces, and also for primitive types (int.class).
Since Class is generic, type of MyClass.class is Class<MyClass>, so that you can do this:
Class<MyClass> c = MyClass.class;
MyClass o = c.newInstance(); // No cast needed, since newInstance() on Class<T> returns T
Methods of Class can be found in its javadoc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With