Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java instanceof operator and Class-returning method

I'm writing my own TableModel implementation. As I shall need a few various implementations sharing some functionality, I decided to prepare an abstract class first. The fields of the table are represented by:

protected Object[][] lines;

Basically all elements in the same column should be of the same type, however column classes may vary among different implementations. I would like to write a common setValueAt function in the abstract class, checking whether val is of proper type or not.

@Override
public void setValueAt(Object val, int row, int col) {
    if (val instanceof this.getColumnClass(col))
        lines[col][row] = val;
}

The compiler signals error here:

Syntax error on token "instanceof", == expected

Why?

like image 393
Sventimir Avatar asked Feb 17 '23 14:02

Sventimir


1 Answers

The right operand of instanceof must be a ReferenceType(JLS 15.20). Use

if (this.getColumnClass(col).isInstance(val))
like image 191
johnchen902 Avatar answered Feb 26 '23 18:02

johnchen902