Reputation: 137
What is the java class
type used for? I am confused about what it means and how it is different than declaring an object
type:
Class className;
Thanks
Upvotes: 5
Views: 333
Reputation: 1108682
It represents the runtime type of the object. The actual programmatic use of the Class
type is often found in reflection and generics.
For example, loading a JDBC driver abstractly with help of Class#forName()
:
String jdbcDriverClassName = getItFromSomeExternalConfigurationFile();
Class.forName(jdbcDriverClassName);
Or typecasting an generic Object
to a beforeknown type:
public static <T> T findAttribute(String key, Class<T> type) {
return type.cast(attributeMap.get(key)); // It's a Map<String, Object>.
}
...which can be used as
SomeType instance = findAttribute("someKey", SomeType.class);
A more extended example can be found here in flavor of a "generic object converter".
Actually, reading the java.lang.Class
javadoc, including all of the available methods, should give you an idea what it can be used for.
Upvotes: 3
Reputation: 4034
From the book Thinking in Java:
The Class object
To understand how Run Time Type Information (RTTI) works in Java, you must first know how type information is represented at run time. This is accomplished through a special kind of object called the Class object, which contains information about the class. In fact, the Class object is used to create all of the 'regular' objects of your class.
Upvotes: 1
Reputation: 41087
Class
is a special type of Object
, i.e Class
is a sub class of Object
. Every class you define has its own Class
object. You can access this as MyObject.class
or myInstance.getClass()
. In another word, any class you define has a Class
attribute where as any class is an Object
. I agree it is slightly confusing to a newbie.
Upvotes: 2
Reputation: 3324
There are several uses for a Class object. For example, say I want to create an instance of a class based on some class name stored in a config file.
String className = config.myClass;
Class clazz = Class.forName(className);
Object myClassInstance = clazz.newInstance();
Upvotes: 4
Reputation: 11213
You can use it when checking the type of some variable or check for inheritance runtime. It's also used in reflection, to load dynamically types and execute methods on them.
Upvotes: 1
Reputation: 39733
javadoc says:
Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.
Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.
Upvotes: 1