Variable declaration not allowed. But there is no scope overlap

user6679234

When I fill an array of characters I get an error.

     List<Character> letters = new ArrayList<>();
     int i = 0;
     if (i == 0) {
         Character str = 'a';
         letters.add(str);
     } else if (i == 1)
         Character str = 'b'; //Variable declaration not allowed here
         letters.add(str);
     }

Why does this error message appear? Cases i==0 and i==1 are mutually exclusive. Why compiler doesn't allow me to decare str the second time?

sixtytrees

Scope in not overlapping, this is correct. You are forgetting an opening curly bracket at else if line. This is just a typo that looks like

} else if (i == 1){ fixes a problem.

Your situation is distinctly different from JAVA Variable declaration not allowed here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

variable declaration not allowed here

From Dev

Variable declaration not allowed in Java

From Dev

variable declaration not allowed here

From Dev

Java variable declaration not allowed

From Dev

Scope of a 'for' loop at declaration of a variable

From Dev

Why is variable declaration not allowed here?

From Dev

Why is ;; allowed after a local variable declaration, but not after a field declaration?

From Dev

Difference between "variable declaration" and "short variable declaration" at local scope in Go

From Dev

Node.js variable declaration and scope

From Dev

Why isn't short variable declaration allowed at package level in Go?

From Dev

Local extern variable declaration with using-directive in the nearest enclosing scope

From Dev

Duplicate declaration in the current scope error; two loops with the same variable, variable scope

From Dev

Global variable inline assignment with combined declaration/assignment operator and another undeclared variable losing scope?

From Dev

Why is a forward declaration in a function declaration allowed?

From Dev

Declaration and declaration with definition. Why is this not allowed?

From Dev

extern declaration and global scope

From Dev

scope of function declaration in c

From Dev

Duplicate declaration in current scope

From Dev

Declaration difference and scope?

From Dev

Swift 3 - declaration no longer allowed

From Dev

Can two local variable blocks call each other? Scope issue due to order of declaration

From Dev

Typescript Service not working with an err of block scope variable 'place' used before its declaration

From Dev

Why does accessing a private variable in another closure's scope via prototypical inheritance allowed?

From Dev

Friend function scope and point of declaration

From Dev

Javascript class declaration scope confusion

From Dev

Scope of allowed functions/macros in Clojure

From Dev

Scope of variable

From Dev

Constexpr is not allowed in declaration of friend template specialization?

From Dev

Is a class declaration allowed after a class definition?

Related Related

  1. 1

    variable declaration not allowed here

  2. 2

    Variable declaration not allowed in Java

  3. 3

    variable declaration not allowed here

  4. 4

    Java variable declaration not allowed

  5. 5

    Scope of a 'for' loop at declaration of a variable

  6. 6

    Why is variable declaration not allowed here?

  7. 7

    Why is ;; allowed after a local variable declaration, but not after a field declaration?

  8. 8

    Difference between "variable declaration" and "short variable declaration" at local scope in Go

  9. 9

    Node.js variable declaration and scope

  10. 10

    Why isn't short variable declaration allowed at package level in Go?

  11. 11

    Local extern variable declaration with using-directive in the nearest enclosing scope

  12. 12

    Duplicate declaration in the current scope error; two loops with the same variable, variable scope

  13. 13

    Global variable inline assignment with combined declaration/assignment operator and another undeclared variable losing scope?

  14. 14

    Why is a forward declaration in a function declaration allowed?

  15. 15

    Declaration and declaration with definition. Why is this not allowed?

  16. 16

    extern declaration and global scope

  17. 17

    scope of function declaration in c

  18. 18

    Duplicate declaration in current scope

  19. 19

    Declaration difference and scope?

  20. 20

    Swift 3 - declaration no longer allowed

  21. 21

    Can two local variable blocks call each other? Scope issue due to order of declaration

  22. 22

    Typescript Service not working with an err of block scope variable 'place' used before its declaration

  23. 23

    Why does accessing a private variable in another closure's scope via prototypical inheritance allowed?

  24. 24

    Friend function scope and point of declaration

  25. 25

    Javascript class declaration scope confusion

  26. 26

    Scope of allowed functions/macros in Clojure

  27. 27

    Scope of variable

  28. 28

    Constexpr is not allowed in declaration of friend template specialization?

  29. 29

    Is a class declaration allowed after a class definition?

HotTag

Archive