Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare object type

Tags:

java

types

I don't know how to perform type checking on newEntry, I want to make sure that it is of type MyTable (without creating an object of MyTable).

public static boolean add(String table, Object newEntry)
{
    boolean result; 
    if (table.equals("MyTable") && newEntry.getClass() == MyTable.getClass())
    {
         ...
    }   
}

My problem is:

newEntry.getClass() == MyTable.getClass(). 

Note: MyTable is a class name, not an object.

like image 397
Don Code Avatar asked Feb 06 '26 21:02

Don Code


1 Answers

Basically what you want is:

isAssignableFrom

Take a look at: http://www.ralfebert.de/blog/java/isassignablefrom/

So, in your case, you want:

MyTable.class.isAssignableFrom(newEntry.getClass())

like image 112
JHollanti Avatar answered Feb 08 '26 11:02

JHollanti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!