Java 如何修复 “class, interface, or enum expected” 错误

Java 如何修复 “class, interface, or enum expected” 错误

无论是初学者还是有经验的Java程序员,在编写代码时都会遇到许多错误。一般来说,这些错误可以分为运行时错误和编译时错误。运行时错误发生在成功编译后运行代码时,而编译时错误发生在编译过程中。

“class, interface, or enum expected” 是在源代码编译过程中抛出的错误。可能由于多种原因导致此错误,其中之一是花括号放置错误。在本文中,我们将探讨这个错误的原因以及修复 “class, interface, or enum expected” 错误的方法。

修复Java中的 “class, interface, or enum expected” 错误

编译错误表示我们的代码不符合Java编程语言的语法规则。编译器生成的 “class, interface, or enum expected” 错误表示我们在代码中写了一些Java编译器不期望的内容。

“class, interface, or enum expected” 错误的原因包括:

  • 花括号错误

  • 未声明类

  • 在类范围之外定义方法

  • 包错误

让我们逐个讨论这些问题以及修复这些错误的方法。

原因1:花括号错误

正如前面提到的,遇到 “class, interface, or enum expected” 错误的最常见原因是额外或放置错误的花括号。也许,由于这个原因,我们经常会看到这个错误,因为程序员经常会忽略花括号。

由于我们需要将代码放在类、接口或枚举中,所以当我们在代码中错误地添加额外的花括号时,Java编译器就会期望一个类、接口或枚举。

示例1

以下示例说明了通过放置花括号错误地出现错误。

public class Example1 {
   public static void main(String[] args) {
      int nums = 0;
      nums++; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }
}
} // adding extra curly brace to generate the error

输出

Example1.java:8: error: class, interface, enum, or record expected
} // adding extra curly braces to generate the error
^
1 error

示例2

以下示例说明了我们可以通过从代码中删除多余的花括号来修复类、接口或枚举错误。

public class Example2 {
   public static void main(String[] args) {
      int nums = 0;
      nums++; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }
}

输出

Incremented value: 1

原因2:未声明类

在某些情况下,可能会忘记定义一个类,并且根本没有将代码封装在类中。在这种情况下,我们可能会遇到类、接口或枚举错误,因为根据Java的指南,每个代码块必须定义在一个类中。因此,请确保将每个代码块封装在一个类中。

原因3:在类作用域外定义方法

导致此错误的另一个原因是错误地将方法定义在类的作用域之外。

示例3

在以下示例中,我们故意将’main()’方法放在类的外部以生成错误。

public class Example3 { }
// from below lines we will get error
   public static void main(String[] args) {
      int nums = 0;
      nums++; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }

输出

Example3.java:3: error: class, interface, enum, or record expected
   public static void main(String[] args) {
                 ^
Example3.java:5: error: class, interface, enum, or record expected
      nums++; // incrementing the value
      ^
Example3.java:6: error: class, interface, enum, or record expected
      System.out.println("Incremented value: " + nums);
      ^
Example3.java:7: error: class, interface, enum, or record expected
   }
   ^
4 errors

示例4

为了修复前面的错误,我们只需将’main()’方法放在类内部,就像示例中演示的那样。

public class Example4 {
   public static void main(String[] args) {
      int nums = 5;
      nums += 1; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }
}

输出

Incremented value: 6

问题4:包的问题

当一个Java程序员在单个源代码中声明多个包时,我们可能会遇到这个问题。

示例5

在这个示例中,我们将声明两个包来生成错误。

package dummy1;
package dummy2;
public class Example5 {
   public static void main(String[] args) {
      int nums = 5;
      nums += 1; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }
}

输出

dummy1/Example5.java:2: error: class, interface, enum, or record expected
package dummy2;
^
1 error

示例6

在这个示例中,我们将移除一个已定义的包来修复类、接口或枚举预期的错误。

package dummy1;
public class Example6 {
   public static void main(String[] args) {
      int nums = 5;
      nums += 1; // incrementing the value
      System.out.println("Incremented value: " + nums);
   }
}

输出

Incremented value: 6

结论

在本文中,我们通过几个示例程序了解了类、接口或枚举预期错误。同时,我们也发现了这个错误的原因以及修复它的方法。错误放置花括号是最常见的原因。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程