Wednesday, April 17, 2013

How to determine the type of object in runtime

Use case :  How to determine the type of a specific object at runtime ?
Solution :

  public void deterMineTypeOfObject(Object obj){
   if(obj instanceof Stop){
          System.out.printlin(" Given Object is type of : " +obj.getClass());
  } else if(obj instanceof Equipment){

          System.out.printlin(" Given Object is type of : " +obj.getClass());
    }
  }

instanceof returns if the given object is same type of specified class. Here Stop and Equipment are two java classes

No comments:

Post a Comment