Example of ´instanceof´
Solution 1
boolean t2 = tableModel.getClass().equals(TableModel1.class); //False
boolean t2 = tableModel.getClass().equals(TableModel2.class); //True
Solution 2
You cannot do it with instanceof
, but you can do it with getClass
:
boolean t1 = tableModel.getClass().equals(TableModel1.class);
boolean t2 = tableModel.getClass().equals(TableModel2.class);
The instanceof
operator is intended for checking class hierarchy all the way, down to the java.lang.Object
, including the checks for all interfaces. It lets you know if an instance of the object you have can be cast to the type you specified without triggering class cast exception.
getClass
, on the other hand, returns the specific class of the given object.
Solution 3
So, how could I differentiate between TableModel1 and TableModel2 using instanceof?
Technically, you can check that tableModel
is an instance of TableModel1
and not and instance of TableModel2
:
(tableModel instanceof TableModel1) && !(tableModel instanceof TableModel2)
However, I would like to encourage you in the strongest possible terms to avoid any code that branches based on the result of instanceof
or getClass()
. Such code is very fragile in the face of future changes. If you find yourself doing anything along these lines, it's a strong clue that it may be a good time to revisit your design.
Solution 4
instance of means "is a".
TableModel2 IS A TableModel1.
But TableModel1 IS NOT A TableModel2.
so
package main;
public class TempClass {
public static void main(String[] args) {
TableModel1 tableModel1 = new TableModel1();
TableModel1 tableModel2 = new TableModel2();
System.out.println(tableModel1 instanceof TableModel1);
System.out.println(tableModel1 instanceof TableModel2);
System.out.println(tableModel2 instanceof TableModel1);
System.out.println(tableModel2 instanceof TableModel2);
}
public static class TableModel1 {
}
public static class TableModel2 extends TableModel1 {
}
}
true
false
true
true
Solution 5
You cannot. If you really need to tell one from another, use tableModel.getClass()
instead, as in:
boolean t1 = tableModel.getClass() == TableModel1.class;
boolean t2 = tableModel.getClass() == TableModel2.class;
Note though you're deliberately trying to break one of founding principles of OOP, so make sure you cannot avoid this trick before you use it in a real code.
Klausos Klausos
Updated on February 08, 2020Comments
-
Klausos Klausos over 4 years
public class TableModel2 extends TableModel1 { ... } TableModel2 tableModel = new TableModel2(); boolean t1 = tableModel instanceof TableModel1; boolean t2 = tableModel instanceof TableModel2;
In the above example,
t1
andt2
aretrue
. So, how could I differentiate betweenTableModel1
andTableModel2
usinginstanceof
? -
Klausos Klausos over 12 yearspublic class TableModel2 extends TableModel1 { ... } Therefore "instanceOf" does not work here
-
alf over 12 yearsWhat happens if I add
TableModel3
?TableModel25
? -
kornero over 12 yearstableModel1 and tableModel2 they are both TableModel1. So instance of give true. But tableModel1 IS NOT A TableModel2.
-
Kanagavelu Sugumar over 8 yearsProvided NULL check is added for tableModel before doing this, where as instanceof doesn't require it.