简体   繁体   中英

Can't set a variable inside a while loop

New to Java, sorry if this is a stupid question. I'd like to assign new values to a couple of variables from inside a while loop, but that's not working. I'm getting an error "The local variable newString may not have been initialized" when trying to compile. Here's an example:

public class Test {

    public static String example() {
        String first;
        String second;
        String newString;
        int start = 1;

        while (start<5) {
            if (start<4) {
                first = "hello";
                second = "world";
                newString = first + second;
            }
            start += 1;
        }
        return newString;
    }

    public static void main(String[] args) {
            System.out.println(example());
    }
}

You are getting this error because the compiler does not know if newString will ever be initialized before it gets returned.

Although you know that both start<5 and start<4 are true and will, thus, execute, the compiler doesn't perform these types of calculations during compilation. Hence, for all it knows, those statements will never execute, and thus newString may get returned before it is initialized.

Hence, you should initialize it (eg to null ) when you declare it to avoid this error.

There is a rule in Java that all local variables must be initialized before they are first read. You are using newString as a return value, which is a read operation.
Although you are assigning a value to newString , it is done in conditional situation ( start<5 && start<4 ). At the compile time, the compiler does not know what will be the result of running the code and it conservatively complains about this situation.

The simple solution will be initializing the string:

 String newString = "";

When you are returning a variable as a result from a method, the compiler needs to be sure that a value would be assigned to that variable (in your case newString ).

Although it is pretty clear for you that the condition start < 4 will be true at some points, the compiler is not intelligent enough to figure that out, which means you have to return only variables, which has values for sure.

Depending on the purpose of your method, you have the following opportunities:

String newString = "";

In this case you are sure that your method will never return null , which could be tricky in some cases for finding errors.

Another opportunity is

String newString = null;

if you want to allow your method to return null values.

As it is obvious in this case that you will eventually enter the if -block and assign a value to the variable newString in other cases it won't be that obvious, so you need to determine whether to allow your method return null values or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM