Tutorials Point


  Java Basics Examples
  Java Tutorial
  Java Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Java Examples - Printing Array using Method


previous next AddThis Social Bookmark Button


Problem Description:

How to display Object class using instanceOf keyword?

Solution:

This example makes displayObjectClass() method to display the Class of the Object that is passed in this method as an argument.

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

public class Main {

public static void main(String[] args) {
   Object testObject = new ArrayList();
      displayObjectClass(testObject);
   }
   public static void displayObjectClass(Object o) {
      if (o instanceof Vector)
      System.out.println("Object was an instance 
      of the class java.util.Vector");
      else if (o 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 " 
      + o.getClass());
   }
}

Result:

The above code sample will produce the following result.

Object was an instance of the class java.util.ArrayList


previous next Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names