“错误:'.class'预期"是什么意思?是什么意思,我该如何解决 [英] What does "error: '.class' expected" mean and how do I fix it

查看:127
本文介绍了“错误:'.class'预期"是什么意思?是什么意思,我该如何解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,新用户会遇到以下不太明显的编译错误'.class' expected:

Occasionally, new users encounter the following rather obscure compilation error '.class' expected:

double d = 1.9;
int i = int d;  // error here
         ^
error: '.class' expected

某些Java IDE编译器对此声明略有不同; e,g,

Some Java IDE compilers state this slightly differently; e,g,

error: insert ". class" to complete Expression 

这样的错误实际上是什么意思,是什么原因导致的,以及如何解决这些问题?

What do errors like this actually mean, what causes them, and how should you fix them?

推荐答案

首先,这是编译错误.如果您在 runtime 看到消息,则可能是正在运行的代码具有编译错误.

First of all, this is a compilation error. If you see the message it at runtime, you are probably running code that has compilation errors.

以下是错误的几个示例:

Here are a couple of examples of the error:

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

int j = someFunction(int[] a);  // error here
                       ^

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

In both cases, the compiler error message will be error: '.class' expected.

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

The compiler has gotten rather confused during syntax checking by some code that is (frankly) nonsensical. The compiler has encountered a type (e.g. int or int[]) in a context where it was actually expecting an expression. It is then saying that the only symbols that would be syntactically acceptable at this point would be . followed by class.

以下是该语法正确的示例;

Here is an example where this syntax would be correct;

Class<?> clazz = int;         // incorrect
Class<?> clazz = int.class;   // correct!

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

Note: It should always be possible to figure out why the compiler's syntax checker thinks the type should be an expression. However, it is often simpler to just treat this as "the compiler is confused" and look for the (inevitable!) syntax error that caused the confusion. That syntax error may not be obvious ... to a beginner ... but knowing that this is the root cause is a good start.

不幸的是,添加.class的建议"几乎总是不正确的.在本答案开头的两个示例中肯定没有帮助!

Unfortunately, the "suggestion" of adding .class is almost always incorrect. It certainly won't help in the two examples at the start of this Answer!

实际的修复方法取决于您尝试通过在此处放置类型来实现的目的.

The actual fix depends on what you were trying to achieve by putting the type there.

  • 如果打算编写 type强制转换,则需要在类型周围加上括号(圆括号).例如

  • If you were intending to write a type cast, then you need to put parentheses (round brackets) around the type; e.g.

double d = 1.9;
int i = (int) d;   // Correct: casts `1.9` to an integer

  • 如果您只是想按原样分配值或传递参数,则应删除该类型.

  • If you were simply intending to assign a value or pass a parameter as-is, then the type should be removed.

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

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

    You need to specify the type of a formal parameter when declaring a method. But you usually don't need to specify it for the actual argument. In the rare situations where you do (to resolve overload ambiguity), you use a type cast.

    someMethod(array[]);
    

    array[]上报告了错误,因为该类型不是表达式.更正可能是:

    The error is reported on array[] because that is a type not an expression. The correction would probably be either:

    someMethod(array);                  // pass ref to the entire array
    

    someMethod(array[someExpression]);  // pass a single array element
    


    int i = someMethod(int j); 
    

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

    The programmer has put a parameter declaration into a method call. An expression is required here, not a declaration:

    int i = someMethod(j);
    


    int i = int(2.0);
    

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

    The programmer was trying to do a typecast. It should be written like this:

    int i = (int) 2.0;
    


    int[]; letterCount = new int[26];
    

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

    The programmer has added a spurious semicolon. It should be written like this:

    int[] letterCount = new int[26];
    


    if (someArray[] > 80) {
        // ...
    }
    

    someArray[]表示类型而不是表达式.程序员可能会说someArray[someIndex] > 80someArray.length > 80之类的东西.

    The someArray[] denotes a type not an expression. The programmer probably means something like someArray[someIndex] > 80 or 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);
    

    这里的错误是"then"语句周围应该有花括号.

    The mistake here is that there should be curly brackets around the "then" statements.

    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);
    }
    

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

    But the compiler's confusion is that the "then" clause of the "if" cannot be a variable declaration. So the parser is looking for an expression that could be a method call. For example, the following would be locally syntactically valid:

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

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

    ... albeit nonsensical in terms of what it tries to do. And of course the compiler would then trip over the dangling else.

    这篇关于“错误:'.class'预期"是什么意思?是什么意思,我该如何解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆