11

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?

2
  • 1
    Read the docs. When using isInstance, it only returns true if the argument is an instance of that class, not if the argument is the same class object. Spell.class.isInstance(new Spell()) returns true. You aren't passing it an instance of that type, so it's returning false
    – Vince
    Jun 26, 2015 at 23:52
  • This is the declaration for java.lang.Class.isInstance() method- public boolean isInstance(Object obj) Jun 26, 2015 at 23:54

4 Answers 4

8

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))
7

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));
1
  • 1
    I think it would be proper to write Someclass instead of Someobject because its the class name that we are specifying here. Jun 26, 2015 at 23:56
4

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));
0
0

isInstance determines if the specified Object is assignment-compatible with the object represented by this Class. You're passing it a class when it expects an object.

The opposite should work:

Spell.class.isInstance(spell)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.