Annotations and Reflection
Class Literal
A class literal gives a program a Class object without creating an instance.
Class Literal
ClassLiteral.java
public class ClassLiteral {
public static void main(String[] args) {
int choice = ;
Class<?> type;
if (choice == 1) {
type = String.class;
} else if (choice == 2) {
type = Integer.class;
} else {
type = Boolean.class;
}
System.out.println("simple=" + type.getSimpleName());
System.out.println("package=" + type.getPackageName());
}
}
public class ClassLiteral {
public static void main(String[] args) {
int choice = ;
Class<?> type;
if (choice == 1) {
type = String.class;
} else if (choice == 2) {
type = Integer.class;
} else {
type = Boolean.class;
}
System.out.println("simple=" + type.getSimpleName());
System.out.println("package=" + type.getPackageName());
}
}
public class ClassLiteral {
public static void main(String[] args) {
int choice = ;
Class<?> type;
if (choice == 1) {
type = String.class;
} else if (choice == 2) {
type = Integer.class;
} else {
type = Boolean.class;
}
System.out.println("simple=" + type.getSimpleName());
System.out.println("package=" + type.getPackageName());
}
}
class literal
The `.class` syntax asks Java for metadata about a type.
Class object
A Class object describes a type and exposes names, package data, methods, fields, and constructors.