简体   繁体   English

为什么eclipse会让我的return语句出错?

[英]Why does eclipse give me an error with my return statement?

I am trying to make the method selectVMwareServer return a VMwareServer object when the key is is present in the HashMap. 当密钥存在于HashMap中时,我试图使方法selectVMwareServer返回一个VMwareServer对象。 But eclipse does not like the where I am placing my return statement. 但是eclipse不喜欢我放置return语句的位置。 Why is this? 为什么是这样? Is it because the method HAS TO return an object? 是否因为该方法必须返回对象?

Here is my code; 这是我的代码;

package configFS;

import java.util.HashMap;
import java.util.Map;

public class Config {

    private Map <String, Object> config;

    public Config() {

        /*
         * This will create a new configuration everytime we create a new instance of Config.class
         * By using the generics of String and Object I can link a name tag to any object needed.
         */
        config = new HashMap <String, Object> ();

    }

    public void addVMwareServer(String par1) {

        config.put(par1, new VMwareServer());

    }

    public void removeVMwareServer(String par1) {

        config.remove(par1);

    }

    public VMwareServer selectVMwareServer(String par1) {

        if (config.containsKey(par1)) {

            return (VMwareServer) config.get(par1);

        }

        return null;

    }

}

NOTE: This is very much a work in progress! 注意:这是一项正在进行的工作!

To silence eclipse I let it add the "return null;" 为了使月食静音,我让它添加“ return null;”。

A method should always have a return statement if it has a return type. 如果方法具有返回类型,则应始终具有return语句。 According to your code, if the "if" condition is not satisfied then it will never get into that condition and that return statement will never get executed. 根据您的代码,如果不满足“ if”条件,则它将永远不会进入该条件,并且该return语句也将永远不会执行。

That is why you have to have return statement outside the "if" condition. 这就是为什么必须在“ if”条件之外使用return语句的原因。

You specify what to return in case your if statement (contains the key). 您指定万一if语句(包含键)返回的内容。 Think about what to do in case when your if statement does not hold true and is ignored. 考虑一下如果if语句不成立但被忽略时该怎么办。 The method has to return something, that is where your "Null" return statement comes in handy. 该方法必须返回一些东西,这就是您的“ Null”返回语句派上用场的地方。 for example: 例如:

public boolean selectVMwareServer(String par1) { public boolean selectVMwareServer(String par1){

    if (config.containsKey(par1)) {

        return true; //(VMwareServer) config.get(par1);

    }

    return false; // If I dont have this in place, Java has no idea what to do hence it HAS to return something.

}

Better solution may be along the lines of: 更好的解决方案可能是:

create new field say X, 创建一个新字段,例如X,

public void selectVMwareServer(String par1) {

    if (config.containsKey(par1)) {

        X =  (VMwareServer) config.get(par1);

    }
    else//do nothing, 


}

After this you can use X however you need to solve your problem. 之后,您可以使用X,但是您需要解决您的问题。 Hope its somewhat clear.. 希望它有点清楚..

Is it because the method HAS TO return an object? 是否因为该方法必须返回对象?

Basically, yes. 基本上是。

Or more precisely, every "normal termination" path for the program has to return a value that can be assigned to the method's return type. 更准确地说,程序的每个“常规终止”路径都必须返回一个可以分配给方法的返回类型的值。


Note that it is actually possible to implement a non-void method without a return statement at all; 注意,实际上可以实现一个完全没有 return语句的非空方法。 eg 例如

    public int test() {
        throw new SomeUncheckedException(...);
    }

... but this is not generally useful. ...但这通常没有用。 The compiler's path analysis code can figure out that the test method cannot go past the throw statement ... and hence that a return statement following it is not necessary. 编译器的路径分析代码可以确定test方法不能通过throw语句...因此不需要紧随其后的return语句。 (Indeed it would be a compilation error to add one.) (实际上,添加一个将是编译错误。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Eclipse在返回后总是给我一个错误; - Why does Eclipse always give me an error after return; 为什么我的ButtonGroup给了我一个“ <identifier> 预计“错误? - Why does my ButtonGroup give me an “<identifier> expected” error? 为什么命令提示符在编译时给我一个错误代码,而我的 IDE 却没有? - Why does command prompt give me an error code in compilation when my IDE doesn't? 为什么我的输出给了我错误的每个数字的数量? - Why does my output give me the wrong amount of each number? 为什么我的on活动参数给我Required? - Why does my on activity parameters give me Required ; 为什么我的Java RSA加密会给我一个算术异常? - Why does my Java RSA encryption give me an Arithmetic Exception? 为什么maven给我的不同的utf-8字符比eclipse(在eclipse中测试运行,在maven中失败)? - Why does maven give me different utf-8 characters than eclipse (test run in eclipse, fail in maven)? 为什么它给我 BufferUnderflowException - Why does it give me BufferUnderflowException 为什么声明一个新对象会给我一个语法错误? - Why does declaring a new object give me a syntax error? 为什么 heroku 会给我这个“不兼容的类型”错误? - Why does heroku give me this "incompatible type" error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM