How to Fix Error: “Class, Interface, or Enum Expected” ?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The braces in java are served as a scope limit. The beginning and end of a code block are indicated by curly brackets.

Curly brackets cause the compile-time error known as the class interface or enum expected error in Java. This issue typically happens when the program ends with an extra curly brace.

Since everything in Java is programmed inside a class, interface, or enum, if you close the curly bracket for one class and add another, the compiler will assume that another class is starting and will complain about the class, interface, or enum keyword, as illustrated in the following program:

Code explanation If we compile this code it will raise this error

because of extra braces.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Misplaced Curly Braces

The root cause of the “class, interface, or enum expected” error is typically a misplaced curly brace “}”. This can be an extra curly brace after the class. It could also be a method accidentally written outside the class.

Code Explanation The following error will be raised

To solve this error simply remove extra brace

Function Outside Class (Intro+ Add Code Example Ti Fix Error)

A method present outside of a class may also be the cause of this problem. The compilation process will fail since this method does not belong to any class. Moving the method within the class will fix this issue.

Code Explanation In Above code method1 is outside of class classb this will raise error.To solve this placed method1 inside of classb.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Multiple Package

In a Java file, only one package can be declared. The expected compilation error for a class, interface, or enum will occur if we have more packages than necessary.

Code Explanation To solve error import only one package.Each source file can only have one package statement, and it must apply to all of the file's types.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Conclusion

  • The class, interface, or enum expected error occurs due to a few different reasons.
  • This error can occur due to misplaced braces by removing extra braces or adding missing braces this error can be solved.
  • If a method is included outside of a class, then this error can happen. By placing method within class error can be solved.
  • This error can be raised if multiple packages are imported in a java file. It is possible to fix errors by importing just one package.
  • The solution to this problem is rather simple.