Class
class in Reflection API
java.lang
package and is denoted as java.lang.Class<T>
.
Class
type are created automatically by JVM when a class is loaded.
Class.forName("fully.qualified.ClassName")
object.getClass()
ClassName.class
Class.forName("fully.qualified.ClassName")
public class MainApp
{
public static void main(String[] args) throws Exception
{
// Getting Class object using Class.forName()
Class<?> c = Class.forName("java.lang.String");
System.out.println("Class Name : " + c.getName());
System.out.println("Is Interface : " + c.isInterface());
System.out.println("Superclass : " + c.getSuperclass());
}
}
Class Name : java.lang.String Is Interface : false Superclass : class java.lang.Object
object.getClass()
and Using ClassName.class
public class MainApp
{
public static void main(String[] args)
{
// ------ Using getClass() ------
String str = "Hello";
Class<?> c1 = str.getClass();
System.out.println("Class from getClass(): " + c1.getName());
// ------ Using .class ------
Class<?> c2 = String.class;
System.out.println("Class from .class: " + c2.getName());
}
}
Class from getClass(): java.lang.String Class from .class: java.lang.String
Class
class
Class
class are as follows:
S.No | Method | Use |
---|---|---|
1 | getName() |
Returns the fully qualified name of the class. |
2 | getSimpleName() |
Returns the simple name of the class without package name. |
3 | getSuperclass() |
Returns the superclass of the class. |
4 | getInterfaces() |
Returns an array of interfaces implemented by the class. |
5 | getDeclaredFields() |
Returns all fields (including private) declared in the class. |
6 | getDeclaredMethods() |
Returns all methods (including private) declared in the class. |
7 | getDeclaredConstructors() |
Returns all constructors (including private) declared in the class. |
8 | getModifiers() |
Returns the modifiers (public, private, abstract, etc.) of the class as an int. |
9 | isInterface() |
Checks if the Class object represents an interface. |
10 | isArray() |
Checks if the Class object represents an array type. |
11 | newInstance() |
Creates a new instance of the class (no-arg constructor only). |
12 | getPackage() |
Returns the package of the class. |
13 | getDeclaredAnnotations() |
Returns annotations present on the class. |
import java.lang.reflect.*;
class Employee
{
public String name;
private int salary;
public Employee() {}
public Employee(String name, int salary)
{
this.name = name;
this.salary = salary;
}
public void work()
{
System.out.println(name + " is working");
}
private void secret()
{
System.out.println("Secret salary is: " + salary);
}
}
public class MainApp3
{
public static void main(String[] args)
{
Class<?> clazz = Employee.class;
// Print basic information
System.out.println("Class Name: " + clazz.getName());
System.out.println("Simple Name: " + clazz.getSimpleName());
System.out.println("Superclass: " + clazz.getSuperclass().getName());
// Print interfaces
System.out.println("\n------ Interfaces ------");
for (Class<?> i : clazz.getInterfaces())
{
System.out.println(i.getName());
}
// Print fields
System.out.println("\n------ Declared Fields ------");
for (Field f : clazz.getDeclaredFields())
{
System.out.println(f.getName() + " : " + f.getType().getSimpleName());
}
// Print methods
System.out.println("\n------ Declared Methods------");
for (Method m : clazz.getDeclaredMethods())
{
System.out.println(m.getName());
}
// Print constructors
System.out.println("\n------ Declared Constructors ------");
for (Constructor<?> c : clazz.getDeclaredConstructors())
{
System.out.println(c);
}
}
}
Class Name: Employee Simple Name: Employee Superclass: java.lang.Object ------ Interfaces ------ ------ Declared Fields ------ name : String salary : int ------ Declared Methods------ secret work ------ Declared Constructors ------ public Employee() public Employee(java.lang.String,int)
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.