两件事:Eclipse 说“必须是静态的”为了一切!并且此方法必须返回 boolean 类型的结果,而它返回 true

发布于 2024-12-13 06:18:06 字数 1237 浏览 0 评论 0原文

这是我用于测试目的的简单代码。

boolean isMoving(){
    if (a == b) {
        return true;
    } else if (a != b) {
        return false;
    }
}

我收到错误:

该方法必须返回布尔类型的结果

但在任何给定时间它必须返回 true 或 false,并且绝不会同时返回两者。还, 无论我在做什么,Eclipse 都会告诉我

无法对非静态现场 bikeSpeed 进行静态引用

因为

void speedUp(){
    bikeSpeed++;
}

bikeSpeed

int bikeSpeed = 2;

可能是简单的答案,但有人可以帮忙吗?我知道如何使用布尔值等,并且从未遇到过问题,但最简单的事情 ^^^ 不起作用。

编辑:这是我的加速:

void speedUp(){
    bikeSpeed++;
}

这是我对它的用法:

    System.out.println("Simple if && if-then-else testing.");
    if (isMoving == true) {
        System.err.println("You're already moving fast, no need to speed up!");
    } else {
        speedUp();
        System.out.println("Time to speed up!");
    }

再次编辑: isMoving 声明现在很好,但是该行 如果(正在移动== true){ 给出错误: isMoving 无法解析为变量。如果我将 isMoving() 切换到它,它也会如此。 最后一个错误是

speedUp();

给出: 无法从 HelloWorld 类型对非静态方法 speedUp() 进行静态引用

编辑上次:我讨厌这个修复,因为它对我来说似乎不正确,但 eclipse 似乎认为使每个单个变量静态修复它(确实如此)即使它不是静态使用的。

Heres my simple code for testing purposes.

boolean isMoving(){
    if (a == b) {
        return true;
    } else if (a != b) {
        return false;
    }
}

I get the error:

This method must return a result of type boolean

While at any given time it must either return true or false, and never returns both. Also,
no matter what I am doing, Eclipse tells me

Cannot make a static reference to the non-static field bikeSpeed

for

void speedUp(){
    bikeSpeed++;
}

with bikeSpeed being

int bikeSpeed = 2;

Probably simple answer, but could anyone help? I know how to use booleans and such, and have never had a problem, but the simplest of things ^^^ is not working.

EDIT: Heres my speedUP:

void speedUp(){
    bikeSpeed++;
}

And heres my usage of it:

    System.out.println("Simple if && if-then-else testing.");
    if (isMoving == true) {
        System.err.println("You're already moving fast, no need to speed up!");
    } else {
        speedUp();
        System.out.println("Time to speed up!");
    }

EDIT AGAIN: The isMoving declaration and such is fine now, but the line
if (isMoving == true) {
gives the error: isMoving cannot be resolved to a variable. So does isMoving() if i switch it to that.
The last error is

speedUp();

gives: Cannot make a static reference to the non-static method speedUp() from the type HelloWorld

EDIT lAST TIME: I hate this fix, since it doesnt seem correct to me, but eclipse seems to think that making EVERY SINGLE VARIABLE static fixes it (which it did) even though it wasnt used statically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夜夜流光相皎洁 2024-12-20 06:18:06
boolean isMoving(){
    if (a == b) {
        return true;
    } else if (a != b) {
        return false;
    }
}

这里的编译器没有检测到这两个分支之一必须被命中,因此它认为存在一种可能的状态,在这种状态下,您可以在不指定返回值的情况下到达方法的末尾。您可以使用 else 代替 else if 或仅 return a == b

这不仅仅是编译器的缺陷;您可能会遇到这样的情况:另一个线程正在后台更改 ab 的值,并且根据时间的不同,这两个条件在评估时可能都为 false 。

boolean isMoving(){
    if (a == b) {
        return true;
    } else if (a != b) {
        return false;
    }
}

The compiler here is not detecting that one of those two branches must be hit, and therefore it thinks that there is a possible state where you would get to the end of the method without specifying a return value. You can use else instead of else if or just return a == b.

That's not just a compiler deficiency; you could have a scenario where another thread is changing the values of a and b in the background, and depending on the timing both of those conditions might be false when they are evaluated.

不必在意 2024-12-20 06:18:06

这是等价的:

boolean isMoving(){
    return a == b;
}

Eclipse 正在抱怨,因为它认为并非所有执行路径都是句柄。尝试:

boolean isMoving(){
    if (a == b) {
        return true;
    } else if (a != b) {
        return false;
    } else {
        return //a is neither equal nor not equal to b???
    }
}

请注意,最后一个分支实际上永远不会发生,因为如果不满足 a == b 条件,则 a != b 必须 为 true 。

This is equivalent:

boolean isMoving(){
    return a == b;
}

Eclipse is complaining because it thinks that not all execution paths are handle. Try:

boolean isMoving(){
    if (a == b) {
        return true;
    } else if (a != b) {
        return false;
    } else {
        return //a is neither equal nor not equal to b???
    }
}

Note that the last branch can actually never happen because if a == b condition is not met than a != b must be true.

余厌 2024-12-20 06:18:06

对于这段代码:

boolean isMoving(){
    if (a == b) {
        return true;
    } else if (a != b) {
        return false;
    }
}

编译器如何可靠地推断出您的 if 语句是互补的?毕竟,另一个线程(理论上)不可能更改 ab 的值吗?那么您可能会遇到没有返回值的情况。

只需提供一个 else(在本例中,使用它代替 else if):

boolean isMoving(){
    if (a == b) {
        return true;
    } else {
        return false;
    }
}

您还可以依赖第一个 return将阻止其余代码执行:

boolean isMoving(){
    if (a == b) {
        return true;
    }

    return false;
}

For this code:

boolean isMoving(){
    if (a == b) {
        return true;
    } else if (a != b) {
        return false;
    }
}

How would the compiler reliably infer that your if statements are complementary? After all, couldn't another thread (in theory) possibly change the value of a or b? Then you might have a case where you aren't returning a value.

Simply provide an else (and in this case, use that instead of else if):

boolean isMoving(){
    if (a == b) {
        return true;
    } else {
        return false;
    }
}

You can also rely on the fact that the first return will prevent the rest of the code from executing:

boolean isMoving(){
    if (a == b) {
        return true;
    }

    return false;
}
魔法唧唧 2024-12-20 06:18:06

对于第一个代码片段,您可以这样做

boolean isMoving(){
    if (a == b) {
        return true;
    } else {
        return false;
    }
}

,甚至

boolean isMoving(){
    return a == b;
}

在原始代码中,java编译器无法确定输入了任何 if 条件,因此当它在末尾找不到返回时会出错,

您也可以将 assert false ;throw new RuntimeException(); 在最后,但在这种情况下这是不必要的

for the first code snippet you can just do

boolean isMoving(){
    if (a == b) {
        return true;
    } else {
        return false;
    }
}

or even

boolean isMoving(){
    return a == b;
}

in your original code the java compiler cannot be sure any if condition is entered so it errors when it can't find a return at the end

you can also put a assert false; or throw new RuntimeException(); at the end but this is unnecessary in this case

半透明的墙 2024-12-20 06:18:06

您可以简化您的代码,

return a == b

但它抱怨的原因是您的代码相当于

boolean isMoving(){
    if (a == b) {
        return true;
    } else if (a != b) {
        return false;
    } else {
        //oh no, nothing there!
    }
}

静态引用意味着您正在从没有类实例的地方访问类字段。您可以从静态的 main 方法访问该字段,而不使用该类的实例。

You could simplify your code to

return a == b

but the reason it's complaining is that your code is equivalent to

boolean isMoving(){
    if (a == b) {
        return true;
    } else if (a != b) {
        return false;
    } else {
        //oh no, nothing there!
    }
}

The static reference means that you're accessing a class field from somewhere you don't have an instance of the class. You could be accessing that field from your main method, which is static, without using an instance of the class instead.

过去的过去 2024-12-20 06:18:06

是的,在某些情况下,编译器无法告诉您永远不会到达块的末尾。
或者你也可以直接返回 false;在右大括号之前。

Yea, there's a case where the compiler can't tell you'll never get to the end of the block.
Alternatively you could just put a return false; before the closing brace.

冷了相思 2024-12-20 06:18:06

对于第一个问题,按如下方式更改代码:

return a == b;

对于第二个问题,代码中是否存在名称冲突?如果您使用:

void speedUp(){
    this.bikeSpeed++;
}

相反,它可以解决您的问题吗?如果是,那么您有两个同名的变量,您不应该这样做。

For your first problem, change your code as follows:

return a == b;

For your second problem, is there a name clash in your code? If you use:

void speedUp(){
    this.bikeSpeed++;
}

instead, does it fixes your problem? If yes, then you have two variables with the same name, which you should not be doing.

戏舞 2024-12-20 06:18:06

第一个问题有点烦人(但有一个原因 - 多线程),java编译器发现并没有覆盖所有基础,修复:

boolean isMoving(){
    if (a == b) {
        return true;
    }

    return false;
}

第二个问题你需要引用对象:

void speedUp(){
    this.bikeSpeed++;
}

编辑:
添加了多线程信息。

看起来您遇到的静态问题位于代码的主块中,由于 main 方法是静态的,因此您无法引用对象变量,您需要先创建对象的实例,然后引用该对象的变量。

First problem is a bit annoying (but there is a reason for that - multythreading), java compiler sees that not all the bases are covered, the fix:

boolean isMoving(){
    if (a == b) {
        return true;
    }

    return false;
}

in second you need to reference the object:

void speedUp(){
    this.bikeSpeed++;
}

Edit:
Added multithreading info.

Looks the static problems that you have are in the main block of the code, since main method is static you can't reference object variables, you need to create instance of the object first and then reference the variables of that object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文