• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variable Declaration

 
Kedar Sarmalkar
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was writing a code

This is giving me an compile error....

:not a statement
String x = null;
^
: ';' expected
String x = null;

but If I give i.e. with braces.

can anybody explain ?
 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kedar..

as far as i know, if we want to use while, we've to use braces..
if we use if statement, we could use it without brace, but only one line after it would be executed..



please correct me if i'm wrong..
Thanks..
 
Kedar Sarmalkar
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leonardo Carreira wrote:Hi Kedar..

as far as i know, if we want to use while, we've to use braces..
if we use if statement, we could use it without brace, but only one line after it would be executed..

please correct me if i'm wrong..
Thanks..


you should try



To rephrase my question... so that it becomes simple to understand.

Why is declaration not allowed in while and also if when it is without braces.

for eg in your code try doing
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JLS
Every local variable declaration statement is immediately contained by a block.

Your declaration in the while or if is not in a block. Also if you think about it, the scope of your declaration is the while loop itself, and since there are not braces, the declaration is of no use

So its also logical that the declaration is not allowed...
 
Kedar Sarmalkar
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit,

Every local variable declaration statement is immediately contained by a block.

I did not understand this statement.... if you can elaborate it would be really great or maybe provide some link.

Ankit Garg wrote:Also if you think about it, the scope of your declaration is the while loop itself, and since there are not braces, the declaration is of no use

So its also logical that the declaration is not allowed...


I thought about your explanation before posting this but if I give braces as in



still declaration of s is of no use according to your explanation.... why does not compiler complain then.

 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, something new to learn
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kedar Sarmalkar
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kedar Sarmalkar
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic