Nameless variable declaration - why does it work?

Sachin Verma

I am surprised to see this behaviour.

Is it a bug or something?

for(Object obj = new Object(), Integer = new Integer(300);
    obj.toString().length()>3;
    System.out.println("on object's loop")) {

} //causes an infinite loop (not foreach loop, of course)

above code compiles and run fine without any reference to new Integer(300). Why so?

I am only interested in knowing why Integer = new Integer(300); is okay without any reference.

arshajii
Object obj = new Object(), Integer = new Integer(300);

This creates two variables:

  1. obj of type Object, which gets assigned to new Object().
  2. Integer (yes, that's the name of the variable) also of type Object, which gets assigned to new Integer(300).

By the way this has nothing to do with the for-loop; that line would compile fine on its own. Now, if that , was really a ;, it would be a different story.

In general, we can construct valid statements of the form:

Type t1 = ..., t2 = ..., t3 = ..., ...;

which is equivalent to

Type t1 = ...;
Type t2 = ...;
Type t3 = ...;
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does this locals variable declaration not work?

From Dev

Why does this locals variable declaration not work?

From Dev

Why does the first one work and the second one not work? Global and Private variable declaration

From Dev

Why declaration of variable in closure environment did not work?

From Dev

How does a variable declaration work as a function?

From Dev

Why does forward declaration of struct not work?

From Dev

C function declaration syntax - why does this work?

From Dev

Why does this use of comma work in a expression but fail in a declaration?

From Dev

Warning: implicit declaration of function — why does my code work anyway?

From Dev

Forward Declaration of class as template param. Why does this work?

From Dev

WHY does this simple Hive table declaration work? As if by magic

From Dev

Why does JavaScript variable declaration at console results in "undefined" being printed?

From Dev

Why does empty variable declaration of type show compile error but not for null declaration?

From Dev

Why does variable expansion without $ work in expressions?

From Dev

Why string variable in JSON does not work?

From Dev

Why does changing a variable from a lambda not work?

From Dev

Why is a function declaration not a statement while a variable declaration is

From Dev

Why does BeautifulSoup .children contain nameless elements as well as the expected tag(s)

From Dev

Javascript variable declaration, why is this legal?

From Dev

Why is variable declaration not allowed here?

From Dev

Why does printing a variable in global scope work, but modifying it does not?

From Dev

Why does printing a variable in global scope work, but modifying it does not?

From Dev

In C, if objects declared at block scope have no linkage, why does function declaration inside main() without "extern" work?

From Dev

Why does explicit method specialization of a template class work without its prototype declaration inside the class

From Dev

Why does the browser try to use an otherwise invalid property declaration when I introduce a CSS variable?

From Dev

Shell, bash, linux ; after variable declaration, why semi-colon and what does it do?

From Dev

Why does the compiler not complain when the struct keyword is reused in a function, with a variable declaration?

From Dev

Why does passing a variable by reference not work when invoking a reflective method?

From Dev

Why does this workaround for magic Perl internal variable work?

Related Related

  1. 1

    Why does this locals variable declaration not work?

  2. 2

    Why does this locals variable declaration not work?

  3. 3

    Why does the first one work and the second one not work? Global and Private variable declaration

  4. 4

    Why declaration of variable in closure environment did not work?

  5. 5

    How does a variable declaration work as a function?

  6. 6

    Why does forward declaration of struct not work?

  7. 7

    C function declaration syntax - why does this work?

  8. 8

    Why does this use of comma work in a expression but fail in a declaration?

  9. 9

    Warning: implicit declaration of function — why does my code work anyway?

  10. 10

    Forward Declaration of class as template param. Why does this work?

  11. 11

    WHY does this simple Hive table declaration work? As if by magic

  12. 12

    Why does JavaScript variable declaration at console results in "undefined" being printed?

  13. 13

    Why does empty variable declaration of type show compile error but not for null declaration?

  14. 14

    Why does variable expansion without $ work in expressions?

  15. 15

    Why string variable in JSON does not work?

  16. 16

    Why does changing a variable from a lambda not work?

  17. 17

    Why is a function declaration not a statement while a variable declaration is

  18. 18

    Why does BeautifulSoup .children contain nameless elements as well as the expected tag(s)

  19. 19

    Javascript variable declaration, why is this legal?

  20. 20

    Why is variable declaration not allowed here?

  21. 21

    Why does printing a variable in global scope work, but modifying it does not?

  22. 22

    Why does printing a variable in global scope work, but modifying it does not?

  23. 23

    In C, if objects declared at block scope have no linkage, why does function declaration inside main() without "extern" work?

  24. 24

    Why does explicit method specialization of a template class work without its prototype declaration inside the class

  25. 25

    Why does the browser try to use an otherwise invalid property declaration when I introduce a CSS variable?

  26. 26

    Shell, bash, linux ; after variable declaration, why semi-colon and what does it do?

  27. 27

    Why does the compiler not complain when the struct keyword is reused in a function, with a variable declaration?

  28. 28

    Why does passing a variable by reference not work when invoking a reflective method?

  29. 29

    Why does this workaround for magic Perl internal variable work?

HotTag

Archive