0
Class<?> b1;
b1 = GS.scr_check_freebox(x, y-GS.ss, this);

if ( b1.getClass().equals(BLK.class) || b1.getClass().equals(STN.class) )
{
    if (b1.falling == true)
    {
        scr_cascade_fall(scr_check_freebox(x, y-GS.ss, this));
        return (true);
    }
}

The function "scr_get_freebox" searches through all instances of BLK and STN and returns the first one it finds that exists wholly or partially inside a box with upper-left corner of the coordinate you pass it, but it will exclude from it's search the "this" that you pass it. It's not really important except that it returns either null or an instance of BLK or an instance of STN.

Giving error on line 5 where is says "if (b1.falling == true)" The thing is, both those classes (BLK and STN) have a boolean variable called falling. The compiler is just being dumb here.

How do i get rid of this error? Is there a special way to use getClass() or instanceof ?

1 Answer 1

1

You have declared b1 as Class<?>. So of course b1.falling == true is an compilation error as Class<?> doesn't have a falling field.

What you want is to change Class<?> b1; to a super class of BLK/STN so it can hold an instance of those classes, in worst case, if there is no other superclass than Object (and later using casts to access fields):

Object b1;
Sign up to request clarification or add additional context in comments.

3 Comments

and how do you cast one object as another?
MyObject mo = (MyObject) obj;
i need a replacement example that uses Object because I casted b1 as BLK or STN depending on which class it came out, and the b1.falling still gives an error.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.