I think java language designers have put some logic here:
if you are writing like this:
Designer must have thought that this if() has no curly braces, means it will have at most one statement.
If that one statement is declaration of any variable, then that variable is of no use, as there is no second statement inside if() scope to use that.
But if you write like this:
Then there is some possibility of second statement for this construct, so they did not give error in this case.
Joined: May 25, 2007
Can somebody confirm... if this works in 1.4 and before.... I never used this style of coding i.e. without braces before but... once while doing a quick code fix... encountered this.
still declaration of s is of no use according to your explanation.... why does not compiler complain then.
Well you didn't use it doesn't mean that it can't be used.
Every local variable declaration statement is immediately contained by a block.
The declarations inside a method is called a local declaration. If we declare s inside a control structure like if or while without blocks, then the declaration basically doesn't have an immediate containing block. The if/while controls the scope of the declaration, so there should be a block enclosing the declaration...
[Well, Punit and Kedar were both faster than me, I took way too long to compose the reply]
Joined: Oct 16, 2008
Kedar Sarmalkar wrote:Can somebody confirm... if this works in 1.4 and before.... I never used this style of coding i.e. without braces before but... once while doing a quick code fix... encountered this.
I think this will work in all version, even it is working in the c# also. And I think all language should be working like this.
Joined: May 25, 2007
@punit : maybe... seems plausible.... but can you confirm this "java designer brain peek" logic from somewhere. BTW this thing is valid in C++ (good old days)
@ankit : even more plausible .... thanks a ton for clearing that out.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com