Saturday, November 23, 2019

How To Possess Class, Interface, Or Enum Expected Fault Inwards Java

If you lot convey always written Java programs using Notepad or within DOS editor, as well as then you lot know that how a unmarried curly span tin dismiss blow your programme as well as throw 100s of fault during compilation. I was ane of those lucky people who started their programming on DOS editor, the blueish window editor which allow you lot 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 piece of work equally much our teacher wants. Since nosotros don't convey the network at that betoken of time, nosotros either hold off for the teacher to come upwardly as well as rescue us as well as nosotros surprise how he solve the fault past times exactly putting ane curly span as well as all errors mysteriously decease away.  Today, I am going to enjoin you lot virtually ane 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 spot is an additional curly span at the destination of the program.

Since everything is coded within a class, interface or enum inwards Java, ane time you lot unopen the curly span for an existing course of report as well as add together to a greater extent than or less other closing curly braces, compiler volition hold off to a greater extent than or less other course of report is starting thus 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 you lot compile this programme using javac, you lot volition larn next error:

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

Since this is a pocket-sized program, you lot tin dismiss easily spot the additional curly span at the destination of the occupation but it's real hard inwards a big programme amongst several classes as well as methods. This becomes fifty-fifty tougher if you lot are non using whatsoever IDE similar Eclipse or Netbeans which volition laissez passer you lot a visible sign of where an fault is. If you lot know how to run Eclipse or whatsoever other Java IDE, exactly copy-paste your code into IDE as well as it volition enjoin you lot the exact place of fault wich hint to solve.

Alternatively, if you lot are coding inwards Notepad, I assume you lot are a beginner, as well as then crusade to correctly indent your code. n our example programme above, discovery that the 2 curly braces at the destination of the programme are at the same indentation level, which cannot occur inwards a valid program. Therefore, but delete ane of the curly braces for the code to compile, the fault volition decease away equally shown below:

$ javac Main.java $

So, adjacent fourth dimension you lot larn the "class, interface, or enum expected" error, exactly cheque if you lot additional curly braces a the destination of your program.

Other Java fault troubleshooting experiences:
  • How to cook "variable mightiness non convey been initialized" fault inwards Java? (solution)
  • Could non exercise the Java virtual motorcar Invalid maximum heap size: -Xmx (solution)
  • Error: could non opened upwardly '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 as well as Solution (steps)

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

No comments:

Post a Comment