How to fix "class, interface, or enum expected" error in Java

If you have ever written Java programs using Notepad or inside DOS editor, then you know that how a single curly brace can blow your program and throw 100s of error during compilation. I was one of those lucky people who started their programming on DOS editor, the blue window editor which allow you to write Java program. I didn't know about PATH, CLASSPATH, JDK, JVM, or JRE at that point. It's our lab computer where everything is supposed to work as much our instructor wants. Since we don't have the internet at that point of time, we either wait for the instructor to come and rescue us and we surprise how he solve the error by just putting one curly brace and all errors mysteriously go away.  Today, I am going to tell you about one such error,  "class, interface, or enum expected".   This is another compile time error in Java which arises due to curly braces. Typically this error occurs when there is an additional curly brace at the end of the program.

Since everything is coded inside a class, interface or enum in Java, once you close the curly brace for an existing class and add another closing curly braces, compiler will expect another class is starting hence it will complain about class, interface, or enum keyword as shown in the following program:

public class Main {

public static void main(String[] args) {
System.out.println("Helloworld");
}
}
}



If you compile this program using javac, you will get following error:

$ javac Main.java
Main.java:14: class, interface, or enum expected
}
^
1 error

Since this is a small program, you can easily spot the additional curly brace at the end of the problem but it's very difficult in a big program with several classes and methods. This becomes even tougher if you are not using any IDE like Eclipse or Netbeans which will give you a visible sign of where an error is. If you know how to use Eclipse or any other Java IDE, just copy-paste your code into IDE and it will tell you the exact location of error wich hint to solve.

Alternatively, if you are coding in Notepad, I assume you are a beginner, then try to correctly indent your code. n our example program above, notice that the two curly braces at the end of the program are at the same indentation level, which cannot happen in a valid program. Therefore, simply delete one of the curly braces for the code to compile, the error will go away as shown below:

$ javac Main.java
$

So, next time you get the "class, interface, or enum expected" error, just check if you additional curly braces a the end of your program.

Other Java error troubleshooting experiences:
  • How to fix "variable might not have been initialized" error in Java? (solution)
  • Could not create the Java virtual machine Invalid maximum heap size: -Xmx (solution)
  • Error: could not open 'C:\Java\jre8\lib\amd64\jvm.cfg' (solution)
  • How to fix java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory (solution)
  • Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger in Java (solution)
  • java.lang.OutOfMemoryError: Java heap space : Cause and Solution (steps)

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

17 comments:

  1. Tq soo much.your explained very clearly. And I am understand very well

    ReplyDelete
  2. Tq soo much.its helped me a lot.and ur explanation is too good

    ReplyDelete
  3. for me the error was missing "class" in the class declaration ;)

    public Main {
    public static void main(String[] args) {
    System.out.println("Helloworld");
    }
    }

    ReplyDelete
    Replies
    1. Yes, because you have missed "class" keyword on public Main, it should be public class Main.

      Delete
  4. //iam getting an error:class ,interface or enum expected.please solve it

    class Demo
    {
    int a;
    double b;
    char c;
    boolean d;
    void show()
    {
    System.out.println(obj.a);
    System.out.println(obj.b);
    System.out.println(obj.c);
    System.out.println(obj.d);
    }
    }
    Class Test 13
    {
    public static void main(String[] args)
    {
    Demo obj=new Demo();
    obj.show();
    }
    }

    ReplyDelete
    Replies
    1. beside other possible errors, you should change capital C to small c in your main class and no space between Test and 13: class Test13

      Delete
    2. Yes, that's correct. Also, paste the exact error as it also has line number and more information to help you better. Thanks Maryam for helping fellow learners.

      Delete

  5. public class ConditionalOrOperator {
    public static void main (String[] args) {
    }

    int age = 31;
    int money = 50;
    }
    if(age > 18 || money > 500)
    System.out.println("Welcome!");





    I am getting error of error: class, interface, or enum expected
    can you help me out ?

    ReplyDelete
    Replies
    1. the code should be inside your main method but that is empty, try this

      public class ConditionalOrOperator {
      public static void main (String[] args) {
      int age = 31;
      int money = 50;
      if(age > 18 || money > 500)
      System.out.println("Welcome!");
      }
      }

      Delete

  6. import java.util.Scanner;
    import java.lang.String;


    //A==1;B==2;C==3
    //if the product (mod 47) of the two vals match then return "go" else "stay"
    public class myclass
    {
    public static double Product (String line1 )
    {
    String x="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int [] Array= new int [line1.length()];
    int counter=0;

    for(int i=0;i<line1.length();i++)
    {
    for(int j=0;j<26;j++)
    {
    if(x.charAt(j)==line1.charAt(i))
    {
    Array[counter]=j;
    counter++;
    }
    }
    }
    double product;
    double constant=Array[0]*Array[1];
    for (int i=2;i<line1.length();i++)
    {
    product = constant *Array[i];
    constant = product;
    }
    return constant;

    }
    public static string Output (String line1, String line2)
    {
    double x= Product(line1);
    double y= Product(line2);
    if(x%47==y%47)
    {
    return "GO";
    } else
    {
    return "STAY";
    }
    }

    }

    public static void main(String [] args )
    {
    String x;
    String y;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a word (all caps)");
    x= scan.nextLine();
    System.out.println("Enter another word (all caps)");
    y=scan.nextLine();

    System.out.println("Output : " + Output(x,y) );

    }


    }
    pls identify errors

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Just past it in an IDE, it will tell you all the errors. You can even correct them live.

      Delete
    2. A quick check to your program shows a lot of error
      1. remove "} from first line, not needed
      2. space between method name is not allowed like one Parameter, add "_" if you want names like that or just use camel case oneParameter

      Delete
    3. here is the correct one
      public class ShapeAreas {
      {
      }public double oneParameter(String shape, float p1)
      {
      float area;
      if (shape.equals("C"))
      return 5 * 5 * Math.PI;
      else if (shape.equals("S"))
      return 5 * 5;
      else
      return -1.0;
      }

      public double twoParameter(String shape, float p1, float p2)
      {
      float area;
      if (shape.equals("R"))
      return p1 * p2;
      else if (shape.equals("T"))
      return 0.5 * p1 * p2;
      else
      return -1.0;
      }
      }

      Delete
    4. This comment has been removed by the author.

      Delete
    5. you can write unit test using JUnit. Use @Test annotation on method and write code inside that.

      Delete
  8. package com.company;
    Class Product{
    //Attributes
    int pid;
    String name;
    int price;
    //Constructors
    Product(){
    System.out.println(">>Product Object Constructed");
    }
    //Methods
    void setProductDetails(int pid, String name, int price){
    this.pid = pid;
    this.String = name;
    this.price = price;

    }
    //read data from Product Object we have in method
    void showProductDetails(){
    System.out.println("------Product ID"+pid+"-----");
    System.out.println("Name"+name);
    System.out.println("Price"+price);
    System.out.println("------------------");
    }

    public class Main {

    public static void main(String[] args) {
    //create new object : Product
    Product product = new Product();
    // Product is not an object is a reference variable that holds the hashcode of yhe object in hexadecimal notation
    System.out.println("product is:"+product);
    }
    // write your code here
    }
    }

    ReplyDelete