Search code examples
javaeclipsepackageinstanceof

Java - Multiple Package In Eclipse Issue


I have the following set of packages within my source folder. The packages are shapes,model,views.

Say I have a class file in my model folder that has the following piece of code:

  shapes.interfaceforshapes[][] temp = model.get2dshapearray();


if(temp[x][y].getClass().isInstance(shapes.cTriangle)){

            }

Please note in the above code temp[x][y] will return a class that interfaces my shapeInterface and all classes within the shapes folder interface this.

Am I doing the correct thing to say "Is the class within my array of type cTriangle"?

I currently get the error:

shapes.cTriangle cannot be resolved to a variable

but I don't want to match a variable, I want to test it agaisnt the class cTriangle within my package shape.

Thanks


Solution

  • Use instanceOf operator if you want to check if the object is an instance of a certain class, while the isInstance method expects an instance of a class.

      if( temp[x][y] instanceOf shapes.cTriangle) {//dosomething }