“错误:预期为'.class'”是什么意思,我该如何解决

Ste*_*n C 1 java compiler-errors

有时,新用户会遇到以下相当晦涩的编译错误'.class' expected

double d = 1.9;
int i = int d;  // error here
         ^
error: '.class' expected
Run Code Online (Sandbox Code Playgroud)

一些Java IDE编译器对此略有不同。例如,

error: insert ". class" to complete Expression 
Run Code Online (Sandbox Code Playgroud)

这样的错误实际上意味着什么,是什么原因导致的,以及如何解决它们?

Ste*_*n C 6

首先,这是一个编译错误。如果在运行时看到该消息,则可能是正在运行的代码具有编译错误。

以下是错误的几个示例:

double d = 1.9;
int i = int d;  // error here
         ^

int j = someFunction(int[] a);  // error here
                       ^
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,编译器错误消息均为error: '.class' expected

错误消息是什么意思,原因是什么?

坦率地说,编译器在语法检查过程中被一些毫无意义的代码弄糊涂了。编译器在实际期望表达式的上下文中遇到了类型(例如intint[])。这就是说,在此.之后在语法上唯一可以接受的符号将是class

这是该语法正确的示例。

Class<?> clazz = int;         // incorrect
Class<?> clazz = int.class;   // correct!
Run Code Online (Sandbox Code Playgroud)

注意:应该总是有可能弄清楚为什么编译器的语法检查器认为类型应该是表达式。但是,将其视为“编译器已混淆”并查找引起混淆的(不可避免的!)语法错误通常更简单。对于初学者来说,语法错误可能并不明显……但是知道这是根本原因是一个好的开始。

您如何解决?

不幸的是,添加的“建议” .class几乎总是不正确的。在本答案开头的两个示例中,它当然无济于事!

实际的修复方法取决于您将类型放在此处以达到的目的。

  • 如果您打算编写一个类型转换,则需要在该类型周围加上括号(圆括号)。例如

    double d = 1.9;
    int i = (int) d;   // Correct: casts `1.9` to an integer
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果您只是打算按原样分配值或传递参数,则应删除类型。

    int j = someFunction(a);  // Correct ... assuming that the type of
                              // 'a' is suitable for that call.
    
    Run Code Online (Sandbox Code Playgroud)

    声明方法时,需要指定形式参数的类型。但是您通常不需要为实际参数指定它。在少数情况下(为了解决过载歧义),请使用类型强制转换。

更多例子

someMethod(array[]);
Run Code Online (Sandbox Code Playgroud)

报告错误,array[]因为它是类型而不是表达式。更正可能是:

someMethod(array);                  // pass ref to the entire array
Run Code Online (Sandbox Code Playgroud)

要么

someMethod(array[someExpression]);  // pass a single array element
Run Code Online (Sandbox Code Playgroud)
int i = someMethod(int j); 
Run Code Online (Sandbox Code Playgroud)

程序员已将参数声明放入方法调用中。这里需要一个表达式,而不是一个声明:

int i = someMethod(j);
Run Code Online (Sandbox Code Playgroud)
int i = int(2.0);
Run Code Online (Sandbox Code Playgroud)

程序员正在尝试进行类型转换。应该这样写:

int i = (int) 2.0;
Run Code Online (Sandbox Code Playgroud)
int[]; letterCount = new int[26];
Run Code Online (Sandbox Code Playgroud)

程序员添加了一个虚假的分号。应该这样写:

int[] letterCount = new int[26];
Run Code Online (Sandbox Code Playgroud)
if (someArray[] > 80) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

someArray[]表示类型不是表达式。程序员可能表示类似someArray[someIndex] > 80或的意思someArray.length > 80


if ((withdraw % 5 == 0) && (acnt_balc >= withdraw + 0.50))
    double cur = acnt_balc - (withdraw + 0.50);
    System.out.println(cur);
else
    System.out.println(acnt_balc);
Run Code Online (Sandbox Code Playgroud)

这里的错误是“ then”语句周围应该有花括号。

if ((withdraw % 5 == 0) && (acnt_balc >= withdraw + 0.50)) {
    double cur = acnt_balc - (withdraw + 0.50);
    System.out.println(cur);
} else {
    System.out.println(acnt_balc);
}
Run Code Online (Sandbox Code Playgroud)

但是编译器的困惑是“ if”的“ then”子句不能是变量声明。因此,解析器正在寻找一个可能是方法调用的表达式。例如,以下内容在本地语法上是有效的:

if ((withdraw % 5 == 0) && (acnt_balc >= withdraw + 0.50))
    double.class.newInstance();   // no compilation error here
Run Code Online (Sandbox Code Playgroud)

...尽管就其尝试而言是荒谬的。当然,然后编译器会越过悬空else