Determining class with a Java instanceOf example – J032

by Oct 21, 2015

DeegeU Java Course

The “Determining class with a Java instanceOf example” video is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.

Transcript – Determining class with a Java instanceOf example

So we know how to create an array of related subclasses as one class. We learned that through polymorphism we can treat every class in the array as the superclass. What if we want to go the other direction? What if we need to know the specific class for an instance in our list? In this lesson we’re going to take a quick look at how we can determine the class of an instance.

When we need to test an instance to see what class it belongs to, we can use the keyword instanceof. This keyword acts as a binary operator. It takes an instance and a class as the two operators. The instance is any object variable, and class is any java.lang.Class object.

if (sprite[42] instanceof Sprite) {
    // case instance to Sprite and do something with it
    Sprite mySprite = (Sprite) sprite[42];

We provide the instance we want to test, in this case sprite[42]. We use the keyword instanceof, and finally we provide the class we are testing against. In this Java instanceof example, “com.deege.Sprite”. This expression returns a boolean.

We’d use it in a boolean test like this. In this code, we’re testing to see if the variable sprite[42] is indeed a sprite. If it is, we cast it to a new variable and do something with it. This is a common pattern you’ll see when working with an instance, where you’re not sure of the actual type.

If we test any instance against java.lang.Object, we will always get true.

However, if we test null against any class including Object, we will always get false. This makes sense since all classes in Java are subclasses of Object, and null is not a subclass of any object. Remember it just means nothing. This is a good thing to remember. instanceof operations do not need explicit null checks. If the instance you’re testing is null, the code will always return false.

The other way we can test is just ask the class. Every class has a method getClass(). This method is defined in the class Object, and it returns a class called Class. The bracket T stuff after the class Class is something we’ll get to when we cover Generics. You don’t really need to understand it right now. getClass() will tell you the class you’re working with. Once you have a Class instance, you can even ask what the parent class is. The Class class has a method called getSuperclass().

// Just ask the class what it is
Class<T> clazz = sprite[42].getClass();

// Who's your parent class?
Class<T> parentClass = sprite[42].getParentClass();

So what happens when we call getSuperclass() on the Class Object? We get null. That makes sense since Object is the top class in every hierarchy.

That give us two ways to determine the class of an instance. We can test against a known class as we saw in the java instanceof example using the instanceOf keyword, or we can just ask the object what class it is. Which method you use, really depends on what you’re trying to accomplish.

If you have any questions let me know in the comments. If you liked this video, make sure you like and share. This helps get the video in front of other viewers! New videos come out each week, so make sure you subscribe. You don’t want to miss a video! Thanks for watching, and I’ll see you in the next video!

<p><script async="" src="//"></script>
<!-- DeegeU - Right Side -->
<ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-5305511207032009" data-ad-slot="5596823779"></ins>
(adsbygoogle = window.adsbygoogle || []).push({});

Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • Background from
  • No infringement intended

Get the code

The source code for “Determining class with a Java instanceOf example” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

<h2>Don't miss another video!</h2> <p>New videos come out every week. Make sure you subscribe!<br><script src="//"></script></p> <div class="g-ytsubscribe" data-channel="deegeu" data-layout="full" data-count="default"></div> <p></p>



DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This