Nameless variable declaration - why does it work?
for-loop, java, loops, object
Solution
Object obj = new Object(), Integer = new Integer(300);
This creates two variables:
- `obj` of type `Object`, which gets assigned to `new Object()`.
- `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 = ...;
...
Problem
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.