Skip to main content

How to Use a Class Literal in Java (String.class)

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

String.class is a class literal: an expression that gives you the Class object describing a type. Libraries use it to ask "which type do you want back?", as in a method that takes String.class and returns a String.

Syntax​

Syntax
Class<<Type>> <variable> = <Type>.class;
PartWhat it does
<Type>.classGives the Class object for that type, such as String.class.
Class<<Type>>The type of that object: a Class that describes <Type>.
Methods on itgetName(), getSimpleName(), cast(value) and more.

The Java Language Specification, section 15.8.2: "A class literal is an expression consisting of the name of a class, interface, array type, or primitive type, or the pseudo-type void, followed by a '.' and the token class."

The Example​

The example takes String.class, prints its full and simple names, and uses it to cast an Object that holds a string back to a String.

Prerequisites​

  • A JDK, the Java Development Kit. java Main.java runs a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which stores String.class in type, prints type.getName() and type.getSimpleName(), then casts the Object "Zara" with type.cast and prints it in upper case.

Main.java
public class Main {
public static void main(String[] args) {
Class<String> type = String.class;
System.out.println(type.getName());
System.out.println(type.getSimpleName());

Object value = "Zara";
String name = type.cast(value);
System.out.println(name.toUpperCase());
}
}
  • String.class is the Class object for String.
  • type.cast(value) checks that value really is a String and returns it typed as one, so toUpperCase() can be called.

Step 2: Run It​

Terminal
java Main.java
Output
java.lang.String
String
ZARA

A zsh terminal where java Main.java prints java.lang.String, String and ZARA.

The command is the same in zsh, bash and PowerShell.

Why Libraries Ask for a Class​

A method can't know at run time which type you want, because generic types are erased. Passing String.class hands it that information, so a call like get("name", String.class) can check the value and return a String, or fail clearly.

Common Mistakes​

MistakeWhat happensDo this instead
Casting a value of another type, as in String.class.cast(42)java.lang.ClassCastException: Cannot cast java.lang.Integer to java.lang.StringAsk for the type the value really has
Writing String.getClass()error: non-static method getClass() cannot be referenced from a static contextWrite String.class, or value.getClass() on an object
Expecting getName() to be shortIt includes the package: java.lang.StringUse getSimpleName()

Troubleshooting​

SymptomLikely causeFix
ClassCastException from a typed getterThe stored value has another typePrint the value's getClass() to see what it is
cannot find symbol at .classThe type isn't importedImport the type

FAQ​

Do primitive types have class literals?​

Yes: int.class is the Class object for int, and void.class exists too.

What's the difference between String.class and value.getClass()?​

String.class names a type you know while writing the code; value.getClass() asks an object at run time what it is.

Conclusion​

Type.class gives the Class object for a type, with methods such as getName(), getSimpleName() and cast(). Pass it to methods that need to know which type to return.

Additional Resources​