“Class, Interface, Or enum Expected” error arises due to additional curly braces at end of Java program.

Considering an example where the file is named as wikitechy.java

[pastacode lang=”java” manual=”public%20class%20wiki%20%7B%20%20%20%20%20%0A%20%20%20%20%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%E2%80%9CWikitechy%20welcomes%20you!%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%201%20error%20found%3A%0A%20%20%20%20File%3A%20wikitechy.java%20%20%5Bline%3A%206%5D%0A%20%20%20%20Error%3A%20class%2C%20interface%2C%20or%20enum%20expected%0A” message=”Java Code” highlight=”” provider=”manual”/]

We need to notice that two curly braces at the end of the program are at the same indentation level, which cannot happen in a valid program. So, if you get this same Class, interface or enum expected error next time, first check if there is an additional curly braces at end of program.

Simply delete one of the curly braces for the code to compile properly and check the code below :

[pastacode lang=”java” manual=”%20public%20class%20wiki%20%7B%20%20%20%20%20%0A%20%20%20%20%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%E2%80%9CWikitechy%20welcomes%20you!%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A” message=”Java Code” highlight=”” provider=”manual”/]

We can also identify this kind of error by pressing CTRL-A (to highlight the entire program) and then TAB (to correctly indent the highlighted code)

Compiler error: “class, interface, or enum expected”

Considering an example where the file is named as wikitechy.java

[pastacode lang=”javascript” manual=”import%20java.util.Random%3B%0Aimport%20java.Math.*%3B%0Aimport%20javax.swing.JOptionPane%3B%0Apublic%20static%20void%20wiki(String%20args%5B%5D)%0A%7B%0A%20%20%20%20%2F%2F%20a%20bunch%20of%20code%0A%7D%0A” message=”Java Code” highlight=”” provider=”manual”/] [ad type=”banner”]

In this program,  class declaration(wiki) is missing . Check the Java code below :

[pastacode lang=”java” manual=”public%20class%20wiki%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20public%20static%20void%20wiki(String%20args%5B%5D)%7B%20…%20%7D%0A%7D%0A” message=”Java Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Categorized in: