java getName() getClass()用法

In java there is a way that makes us enabled to get the object class name at runtime. It can be done by calling the getClass() method on the class object. Then  by calling the method getName() we can get the name of the object class.

In our example java program we have created a class RoseIndia and we have created an object of this class. Now we will be calling method getClass() on this object to get the class. 

roseIndiaObject.getClass() returns the runtime class.  roseIndiaObject.getClass().getName() gets the class name of the created object at runtime. Here is the full java code of program which will be getting the object's class name as follows:

public class RoseIndia
{
  public RoseIndia(){
  System.out.println("Constructor Called");
  }
  public static void main(String args[])
  {
  RoseIndia roseIndiaObject = new RoseIndia();
  System.out.println("Object's Class name =>"+
   roseIndiaObject.getClass().getName());
  }
}

输出:

Constructor Called
Object's Class name =>RoseIndia

tructor Called

Object's Class name =>RoseIndia

 

原文地址:https://www.cnblogs.com/youxin/p/2711043.html