Error: not a statement when declaring a boolean in Java

后端 未结 3 1103
[愿得一人]
[愿得一人] 2021-01-25 10:01

The following code:

boolean continue = false;

Returns the following error:

error: not a statement
  boolean continue = false;
          


        
相关标签:
3条回答
  • 2021-01-25 10:33

    You can't use continue as a name of a variable. It's one of java reserved words.

    0 讨论(0)
  • 2021-01-25 10:42

    You cannot use continue as a variable name because it is a reserved word.

    From the docs:

    The continue statement skips the current iteration of a for, while, or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.

    You can tell that it is a keyword because, when you look at your question, continue has syntax highlighting applied, like boolean and false.

    boolean continue = false;
    

    That would be like writing

    boolean boolean = false;
    

    or

    boolean false = false;
    

    Both of those obviously won't work, so try something else like continuing:

    boolean continuing = false;
    
    0 讨论(0)
  • 2021-01-25 10:55

    Try this instead:

    boolean cont = false;
    

    Or use another name. The point is that in Java, continue is a keyword and it can't be used as a variable name - it's right here in the language specification. For future reference this is what continue is used for:

    The continue statement skips the current iteration of a for, while, or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.

    0 讨论(0)
提交回复
热议问题