Zeel Mehta Zeel Mehta - 1 month ago 6
Java Question

error is class,interface or enum expected? import relational.compare

I have the following code:

package relational;

public class compare
{

public int getMax(int x,int y)
{
if(x>y){

return x;
}

else{
return y;

}

}
}


import relational.compare;

public class Pack
{

public static void main(String a[])
{
int a=7,b=9;
compare ob=new compare();
int max=ob.getMax(a,b);

}


}


This won't compile for some reason.
please help me solve this error.
Is the error class-related, interface or enum-related?
the problem seems to be in import
relational.compare
.

Answer

There are several issue with this code.

  1. If this whole code is in one java file then there cab be only one public class.
  2. Change the name of class from "compare" to "Compare" (C in upper case), because object you are making have 'C' in upper case.
  3. You have two variables with same name 'a', change any one of them.

After these changes your code will work fine.

Comments