Java variable declaration not allowed

Lone_Coder

I've made 2 pieces of code. The first works fine, but the other says the variable declaration not allowed here.

First code(Working)

class Test {
    public static void main(String args[]){
        for(int i=0;i<10;i++) {
            int a[]=new int[10];
        }
    }
}

Second code(Not working)

class Test {
    public static void main(String args[]) {
        for(int i=0;i<10;i++)
            int a[]=new int[10];
    }
}

What is the reason behind it; I think both sections of code are nearly the same.

Jon Skeet

What the reason behind it i think both the codes are nearly same.

Nearly, but not quite. To understand why the compiler is complaining, it's often a good idea to look at the language specification.

The body of a ForStatement has to be a Statement. A local variable declaration isn't an option for a Statement - it's an option for a BlockStatement.

This is very deliberate. It's deliberately to prevent you from writing pointless code. There's no point in declaring a local variable as the sole statement within a for loop or an if statement etc. It will be out of scope everywhere else, so why declare it?

The case where it's the only statement within a block is still pointless, but it would be harder for the language to prevent you from doing that.

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 in Java

From Dev

variable declaration not allowed here

From Dev

variable declaration not allowed here

From Dev

Why is variable declaration not allowed here?

From Dev

Variable declaration not allowed. But there is no scope overlap

From Dev

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

From Dev

Variable declaration and initialization in Java

From Dev

Dynamic variable declaration in Java

From Dev

Java - outer variable declaration

From Dev

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

From Dev

Java one line variable declaration?

From Dev

Java variable declaration current standards

From Dev

What variable to use in Java declaration

From Dev

java.util.Scanner Object Declaration not Allowed Here

From Dev

Java multiple inheritance duplicate variable declaration?

From Dev

Can I change declaration type for a variable in Java?

From Dev

Java: static variable declaration best practices

From Dev

Java Coding standard: multiple variable declaration

From Dev

Java: lambda local variable declaration issue

From Dev

Java: Where to place declaration of final variable

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

Swift 3 - declaration no longer allowed

From Dev

Syntax for separating variable declaration and anonymous class instantiation in android/java

From Dev

Java - Why is it allowed to have Variable Name as Type Name

From Dev

Java, is this a declaration?

From Dev

Constexpr is not allowed in declaration of friend template specialization?

From Dev

Is a class declaration allowed after a class definition?

From Dev

Constraints are not allowed on non-generic declaration

Related Related

  1. 1

    Variable declaration not allowed in Java

  2. 2

    variable declaration not allowed here

  3. 3

    variable declaration not allowed here

  4. 4

    Why is variable declaration not allowed here?

  5. 5

    Variable declaration not allowed. But there is no scope overlap

  6. 6

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

  7. 7

    Variable declaration and initialization in Java

  8. 8

    Dynamic variable declaration in Java

  9. 9

    Java - outer variable declaration

  10. 10

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

  11. 11

    Java one line variable declaration?

  12. 12

    Java variable declaration current standards

  13. 13

    What variable to use in Java declaration

  14. 14

    java.util.Scanner Object Declaration not Allowed Here

  15. 15

    Java multiple inheritance duplicate variable declaration?

  16. 16

    Can I change declaration type for a variable in Java?

  17. 17

    Java: static variable declaration best practices

  18. 18

    Java Coding standard: multiple variable declaration

  19. 19

    Java: lambda local variable declaration issue

  20. 20

    Java: Where to place declaration of final variable

  21. 21

    Why is a forward declaration in a function declaration allowed?

  22. 22

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

  23. 23

    Swift 3 - declaration no longer allowed

  24. 24

    Syntax for separating variable declaration and anonymous class instantiation in android/java

  25. 25

    Java - Why is it allowed to have Variable Name as Type Name

  26. 26

    Java, is this a declaration?

  27. 27

    Constexpr is not allowed in declaration of friend template specialization?

  28. 28

    Is a class declaration allowed after a class definition?

  29. 29

    Constraints are not allowed on non-generic declaration

HotTag

Archive