Wednesday, August 19, 2009



You see this error message because of any one of the followings:

1. You misspelled the keyword class (fully lower case) or you did not write it at all for writing your class.

public Class Test //actual keyword is class
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}

In the above example Class (C is in upper case) is written instead of class (fully lower case).

2. Remember you cannot declare variables outside of class body like below.

int variable; //variable must be declared inside the class body
public class Test
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}

To solve this declare variables inside the class body as below.


public class Test
{
int variable; //variable must be declared inside the class body
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}


3. All methods are also defined within the method body. If you define any method outside of the class body you will see this error message.


public class Test
{
public void myMethod()
{
System.out.println("method inside the class body");
}
} //end of class

public void anotherMethod()
{
System.out.println("method outside of the class body");
}

In the above code, the method anotherMethod is defined after the end of the class. So this error occurred. Most often it occurs just because of incorrect use of {} (curly braces). Check all { are closed with } in the proper place.

4. If you are defining an interface, check you spelled it correctly in fully lowercase.

public Interface Test //actual keyword is interface
{
public void myMethod();

}

In the above code, keyword interface is misspelled with upper I.

5. Same way if you define an enum spell the keyword enum correctly.

public Enum Day //actual keyword is enum
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

In the above example Enum is incorrect as the E is in upper case, it will be lower case e.

If the above solutions do not work for you. Please comment below giving your code. I'll try to respond.

5 comments:

  1. thank you so much buddy!!!

    ReplyDelete
  2. thanks...this page ended an irritating one hour of my dilema ....

    ReplyDelete
  3. for me the above 2 comments + I find this blog most useful compared to other blogs.

    ReplyDelete
  4. package student;
    public class scl_student
    {
    public String name;
    public int sno;
    public double m1,m2,avg,tot;

    public void average()
    {
    avg=tot/2;
    }
    public void total()
    {
    tot=m1+m2;
    average();
    }

    }

    package student;\\line 19
    public class clg_student extends scl_student
    {
    public double labm1,labm2;
    public void total()
    {
    tot=m1+m2+labm1+labm2;
    average();
    }
    public void average()
    {
    avg=tot/2;
    }
    }
    heres my code and I'm getting an error class,interface or enum expected......the error line is shown as line 19 in command prompt...

    ReplyDelete
    Replies
    1. Please write the clg_student class in a separate file.

      Delete