Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Class.isInstance() always returns false

In my GameObject class I have the following method to check if the GameObject would be colliding with another object if it moved to the specified position:

public boolean collisionAt(Vector2d position, Class<? extends GameObject>... exclusions) {
    if (getBounds() == null)
        return false;
    Rectangle newBounds = getBounds().clone();
    newBounds.setPosition(position);
    // Check collisions
    for (GameObject object : new ArrayList<>(gameObjects)) {
        if (object.getBounds() != null && newBounds.intersects(object.getBounds()) && object != this) {
            boolean b = true;
            for (Class<? extends GameObject> exclusion : exclusions) {
                if (object.getClass().isInstance(exclusion))
                    b = false;
            }
            if (b)
                return true;
        }
    }
    return false;
}

I want to allow the program to define exclusions, for example if I don't want this method to return true if it collides with a Spell. But for some reason the Class.isInstance() line always returns false. I even tried this:

System.out.println(Spell.class.isInstance(Spell.class));

and the console outputs false! What's going on here?

like image 935
MCMastery Avatar asked Jun 26 '15 23:06

MCMastery


People also ask

What does isInstance do in Java?

The isInstance() method of java. lang. Class class is used to check if the specified object is compatible to be assigned to the instance of this Class. The method returns true if the specified object is non-null and can be cast to the instance of this Class.

What is the difference between class isInstance and Instanceof operator?

The instanceof operator and isInstance() method both are used for checking the class of the object. But the main difference comes when we want to check the class of objects dynamically then isInstance() method will work. There is no way we can do this by instanceof operator.

How to check instance of a class Java?

The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .

What to use instead of instanceof in Java?

Having a chain of "instanceof" operations is considered a "code smell". The standard answer is "use polymorphism".


3 Answers

From official Javadocs

public boolean isInstance(Object obj)

Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.

You need to pass in the object of class rather than the class itself.

Example

SomeClass object = new SomeClass();
System.out.println(SomeClass.class.isInstance(object));
like image 56
Sneh Avatar answered Oct 03 '22 03:10

Sneh


You need to pass in an instance of the class in question rather than a class literal. E.g.

Spell spell = new Spell();
System.out.println(Spell.class.isInstance(spell));
like image 29
Reimeus Avatar answered Oct 03 '22 03:10

Reimeus


The isInstance tests if the given object is an instance of the Class, not if the given Class is a subclass of the Class.

You have your invocation backwards. You need to test if the gameObject is an instance of one of the exclusion classes.

if (exclusion.isInstance(gameObject))
like image 44
rgettman Avatar answered Oct 03 '22 05:10

rgettman