~/ learn/ comp-308/ cards/ RTTI: Class objects, getClass & class literals
1 of 5

Type the three ways to obtain a Class object

Type the three ways to obtain a Class object

Answer

Class<?> a = obj.getClass(); Class<String> b = String.class; Class<?> c = Class.forName("java.lang.String");

getClass() returns the runtime type of a live object; the .class literal is compile-time and exception-free; Class.forName resolves a name String at runtime and throws ClassNotFoundException.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/cf741a52-f10b-4704-98aa-ba1034b15997/flashcard utf-8 LF