Compiler Error: "Class, Interface, or Enum Expected"

Compiler error: class, interface, or enum expected

You miss the class declaration.

public class DerivativeQuiz{
public static void derivativeQuiz(String args[]){ ... }
}

Android Studio Error: class, interface, or enum expeted

Android Studio throws that error when you have code out of the class declaration.
Your public ArrayAdapter<String> buscaCadastro is out of the class right now. Remove the extra closing curly brace after testeInserirCadastro, that should fix it.

Error .java:23: error: class, interface, or enum expected import java.util.*;

The simple solution, as other people have said, is to split your two public classes into two separate .java files. That will solve both the error you've already encountered and another that you were going to encounter later... but "too many public classes" is not what your Java compiler is complaining about. The real reason you're getting that error is that you can't put import statements after class declarations.

The error message is easy to misread. It seems to be complaining about what you're importing --- probably provoking a response like, "But I am importing a class!". But note where the ^ is pointing --- not at java.util.*, but at the word import:

Calc.java:22: error: class, interface, or enum expected
import java.util.*;
^
1 error

This error is actually complaining about the out-of-place import statement --- it shouldn't be there at all. Once you've declared your first class, it's too late to import anything else. In a roundabout way, it actually says so: By line 22 of Calc.java, nothing can appear at the top level but type declarations --- the "interface, class, or enum" it mentioned --- so encountering a line that starts with import is "unexpected", meaning not allowed.

You can verify this by commenting out the second import (line 22) and compiling. You'll still get errors, but different ones.

As an aside, you are allowed to import java.util.*; more than once. The redundant statements will be ignored... as long as they're in the right place. You can verify this by moving the second import up to around line 3 or 4 and compiling. Again, you'll still get errors, but not about that.

The Correct Order

A single .java file (called a "compilation unit" in the language spec) has the following three parts, which must appear in this order:

  1. Zero or one package declarations.
  2. Zero or more import statements.
  3. Zero or more top-level type declarations, like class, enum, or interface.

From the Java 1.8 Language Specification, Section 7.3: "Compilation Units"]:

CompilationUnit is the goal symbol (§2.1) for the syntactic grammar (§2.3) of Java programs. It is defined by the following productions:

CompilationUnit:
[PackageDeclaration] {ImportDeclaration} {TypeDeclaration}


A compilation unit consists of three parts, each of which is optional[...]

In case that last phrase made you wonder: Yes, a completely empty .java file will compile without warnings... and without producing any .class files.

Solutions

As has been pointed out by others, you can't have more than one top-level public class or other type declaration in a single file. Unless you're willing to nest one of Calc and NumCalc inside the other, they have to split up into Calc.java and NumCalc.java.

Actually, you could sidestep the problem by reducing one class's visibility to package-default, but that's fragile and not the way Java is generally done. If you tried to use the package-default class in any other .java file, even one in the same package, it would fail to compile because it couldn't find the supposedly-package-visible class --- either Calc would be in the wrongly-named NumCalc.java (not where the compiler will look for it), or NumCalc would be hiding out inside Calc.java.

But why bother with all that? You could combine the two classes into one class very easily, and have a more coherent project. (I'm not sure why they're separate classes in the first place.)

Compile attempt gives error that says error: class, interface, or enum expected, but error points to Chinese character?

It looks like your program file has something in it that is being interpreted as a multi-byte character. The character in error seems to be right before the "p" in public which is why the compiler is giving the error message. It is expecting a keyword and getting a Chinese character.

What editor did you use? I think the real problem is something about how your editor is set up. This error message is just a symptom. Another possibility is that your system is set up for interpreting certain byte sequences as Chinese. That would explain "\ " being interpreted as a character and something starting with a curly brace and going through "p" being seen as a sequence of them.

error: class, interface or enum expected at void

You have unnecesarry end brace just before your method onCreate:

 }
^^
@Override
protected void onCreate(Bundle savedInstanceState)

This means that compiler thinks that by that brace, your class definition has completed and then sees a new method definition which should be within another class and hence complains about the same. As a best practice, you should always format your code.



Related Topics



Leave a reply



Submit