What is a class literal in Java?

2019-01-01 15:58发布

问题:

From the Java tutorial:

Finally, there\'s also a special kind of literal called a class literal, formed by taking a type name and appending \".class\"; for example, String.class. This refers to the object (of type Class) that represents the type itself.

To what type of variable can this literal be assigned to?

Please give a small example if possible.

回答1:

Class<String> c = String.class;

Check out the Javadoc for java.lang.Class to see what you can do with one of these little guys - mostly related to reflection



回答2:

To understand that, you have to understand that String is an instance (object) of the class Class. A string literal (e.g. \"I am a string.\") is a notation which represents an instance (object) of the class String, whereas a class literal (e.g. Hashtable.class) is a notation which represents an instance of the class Class.



回答3:

Thanks to the other good answers here, you know what it is, but here\'s a typical usage example that may clarify also:

    private static Logger log = Logger.getLogger(YourClassHere.class);

As the code suggests, this is a line where we\'re initialising a logging framework (in this example, I\'m using the org.apache.log4j package, but the principle extends to other frameworks). The getLogger() method requires a class literal so it knows what it\'s logging (i.e. the current object\'s class).



回答4:

According to the JLS

15.8.2 Class Literals

A class literal is an expression consisting of the name of a class, interface, array, or primitive type followed by a . and the token class. The type of a class literal is Class. It evaluates to the Class object for the named type (or for void) as defined by the defining class loader of the class of the current instance.



回答5:

Some common uses may be found in Class Literals as Runtime-Type Tokens.



回答6:

The literal itself is MyClass. If you write MyClass.class you get a Reference to the class object. If you write new MyClass(), it uses the literal to get you an instance of the class object you get by MyClass.class. From the instance you get the same class object by calling myClassInstance.getClass().

I am not 100% sure, but the literal itself cannot be assigned to any variable. What you can do is getting the name of the class as string and use the reflection framework to create an instance.



回答7:

In examples it is someting like that:

Class myClass = MyClass.class

or

MyClass.class.getResourceAsStream(\"config.properties\");


回答8:

To understand that, you have to understand that String is an instance (object) of its superclass (parent class) Object.

class String\'s instance (object)\'s value is a String literal (e.g. \"I am a string.\") :

class   |  instance (object) |  literal
------------------------------------------------
String  |  instance_name  =  |  \"I am a string.\"

whereas class Object\'s instance (object)\'s value is a Class literal — (e.g. Hashtable.class) which refers to class Hashtable\'s instance (object)

class      |  instance (object) |  literal
------------------------------------------------
Hashtable  |  instance_name     |  Hashtable.


回答9:

When the JVM loads your application classes, it stores them as java.class.Class objects.

So, there are usually several instances of type Class in memory that represents your classes. So you can do something like this:

Class<Bicycle> bicycleClass = Bicycle.class; // returns the object storing your Bicycle class
bicycleClass.getName();  // returns your class name
bicycleClass.getDeclaredMethods();  // returns your (declared) class methods


标签: java literals