简体   繁体   中英

java class types - what do they mean?

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

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();

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.

Class is a special type of Object , ie 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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM