Skip to content
the poor coder

Check Object Is Instance Of Class

Poor Man
Published
Reading time
2 min read
On this page

Check Object Is Instance Of Class#

When working with object-oriented programming, one of the basic tasks is checking whether an object belongs to a certain class or not. This is commonly used for determining the type of an object before performing any operations on it. In Java, there are several ways to check if an object is an instance of a class.

Using instanceof Operator#

The simplest way to check if an object is an instance of a class is by using the instanceof operator. This operator returns true if the object is an instance of the specified class or its subclass, and false otherwise.

Text

class Animal {
    // ...
}

class Dog extends Animal {
    // ...
}

Animal animal = new Animal();
Dog dog = new Dog();

boolean isAnimal = animal instanceof Animal; // true
boolean isDog = dog instanceof Animal; // true
boolean isAnimal2 = animal instanceof Dog; // false
boolean isDog2 = dog instanceof Dog; // true

Using Class.isInstance() Method#

An alternative way to check if an object is an instance of a class is by using the Class.isInstance() method. This method takes an object as a parameter and returns true if the object is an instance of the specified class or its subclass, and false otherwise.

Text

class Animal {
    // ...
}

class Dog extends Animal {
    // ...
}

Animal animal = new Animal();
Dog dog = new Dog();

boolean isAnimal = Animal.class.isInstance(animal); // true
boolean isDog = Animal.class.isInstance(dog); // true
boolean isAnimal2 = Dog.class.isInstance(animal); // false
boolean isDog2 = Dog.class.isInstance(dog); // true

Using Class.cast() Method#

The Class.cast() method can also be used to check if an object is an instance of a class. This method takes an object as a parameter and returns the object casted to the specified class if the object is an instance of that class, and throws a ClassCastException otherwise.

Text

class Animal {
    // ...
}

class Dog extends Animal {
    // ...
}

Animal animal = new Animal();
Dog dog = new Dog();

Animal castedAnimal = Animal.class.cast(animal); // works fine
Dog castedDog = Animal.class.cast(dog); // works fine
Animal castedAnimal2 = Dog.class.cast(animal); // throws ClassCastException
Dog castedDog2 = Dog.class.cast(dog); // works fine

These are the basic ways to check if an object is an instance of a class in Java. Choose the one that suits your needs and coding style.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC