How to fix class, interface, or enum expected fault inwards Java

If yous accept e'er written Java programs using Notepad or within DOS editor, together with then yous know that how a unmarried curly duo tin sack blow your plan together with throw 100s of fault during compilation. I was 1 of those lucky people who started their programming on DOS editor, the blueish window editor which allow yous to write Java program. I didn't know virtually PATH, CLASSPATH, JDK, JVM, or JRE at that point. It's our lab reckoner where everything is supposed to operate equally much our teacher wants. Since nosotros don't accept the network at that indicate of time, nosotros either hold off for the teacher to come upward together with rescue us together with nosotros surprise how he solve the fault yesteryear exactly putting 1 curly duo together with all errors mysteriously give-up the ghost away.  Today, I am going to enjoin yous virtually 1 such error,  "class, interface, or enum expected".   This is to a greater extent than or less other compile fourth dimension fault inwards Java which arises due to curly braces. Typically this fault occurs when at that topographic point is an additional curly duo at the terminate of the program.

Since everything is coded within a class, interface or enum inwards Java, 1 time yous closed the curly duo for an existing degree together with add together to a greater extent than or less other closing curly braces, compiler volition aspect to a greater extent than or less other degree is starting so it volition complain virtually class, interface, or enum keyword equally shown inwards the next program:

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


If yous compile this plan using javac, yous volition instruct next error:

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

Since this is a modest program, yous tin sack easily spot the additional curly duo at the terminate of the work but it's really hard inwards a big plan amongst several classes together with methods. This becomes fifty-fifty tougher if yous are non using whatever IDE similar Eclipse or Netbeans which volition plow over yous a visible sign of where an fault is. If yous know how to purpose Eclipse or whatever other Java IDE, exactly copy-paste your code into IDE together with it volition enjoin yous the exact place of fault wich hint to solve.

Alternatively, if yous are coding inwards Notepad, I assume yous are a beginner, together with then essay to correctly indent your code. n our illustration plan above, uncovering that the 2 curly braces at the terminate of the plan are at the same indentation level, which cannot occur inwards a valid program. Therefore, but delete 1 of the curly braces for the code to compile, the fault volition give-up the ghost away equally shown below:

$ javac Main.java $

So, adjacent fourth dimension yous instruct the "class, interface, or enum expected" error, exactly banking concern gibe if yous additional curly braces a the terminate of your program.

Other Java fault troubleshooting experiences:
  • How to cook "variable mightiness non accept been initialized" fault inwards Java? (solution)
  • Could non exercise the Java virtual machine Invalid maximum heap size: -Xmx (solution)
  • Error: could non opened upward 'C:\Java\jre8\lib\amd64\jvm.cfg' (solution)
  • How to cook java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory (solution)
  • Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger inwards Java (solution)
  • java.lang.OutOfMemoryError: Java heap infinite : Cause together with Solution (steps)

References
Java SE 8 linguistic communication specification
Core Java, Volume 1 - Fundamentals

Subscribe to receive free email updates:

0 Response to "How to fix class, interface, or enum expected fault inwards Java"

Posting Komentar