Java.lang.Object.getClass() 方法
描述
java.lang.Object.getClass() 方法返回对象的运行时类。 该 Class 对象是被表示类的静态同步方法锁定的对象。
声明
以下是 java.lang.Object.getClass() 方法的声明。
public final Class getClass()
参数
NA
返回值
该方法返回 Class 类型的对象,表示该对象的运行时类。
异常
NA
示例
下面的例子展示了 lang.Object.getClass() 方法的使用。
package com.tutorialspoint;
import java.util.GregorianCalendar;
public class ObjectDemo {
public static void main(String[] args) {
// create a new ObjectDemo object
GregorianCalendar cal = new GregorianCalendar();
// print current time
System.out.println("" + cal.getTime());
// print the class of cal
System.out.println("" + cal.getClass());
// create a new Integer
Integer i = new Integer(5);
// print i
System.out.println("" + i);
// print the class of i
System.out.println("" + i.getClass());
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Sat Sep 22 00:31:24 EEST 2012 class java.util.GregorianCalendar 5 class java.lang.Integer

