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

查看:25
本文介绍了“错误:'.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 编译器对此的表述略有不同;例如,

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?

推荐答案

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

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.

  • 如果你打算写一个类型转换,那么你需要在类型周围加上括号(圆括号);例如

  • 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.

    int[] integers = new int[arraySize];
    ...
    return integers[];
    

    integers[] 表示类型声明符,但需要一个表达式.应该是

    The integers[] denotes a type declarator but an expression is required. It should be either

    return integers;             // Return the entire array
                            
    

    return integers[someIndex];  // Return one element of the array
    


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

    但是编译器的困惑在于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天全站免登陆