A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/javaexamples/method_instanceof.htm below:

How to display Object class using instanceOf keyword in Java

How to display Object class using instanceOf keyword in Java 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

java_methods.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4