Finding Out of what Class an Object is Instantiated : Intanceof « Language Basics « Java






Finding Out of what Class an Object is Instantiated

 

import java.util.ArrayList;
import java.util.Vector;

public class Main {

  public static void main(String[] args) {
    Object testObject = new Vector();
    if (testObject instanceof Vector)
      System.out.println("Object was an instance of the class java.util.Vector");
    else if (testObject instanceof ArrayList)
      System.out.println("Object was an instance of the class java.util.ArrayList");
    else
      System.out.println("Object was an instance of the " + testObject.getClass());

  }
}
//Object was an instance of the class java.util.Vector

   
  








Related examples in the same category

1.Get a class of an object
2.The difference between instanceof and classThe difference between instanceof and class