wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes variable Declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "variable Declaration" Watch "variable Declaration" New topic
Author

variable Declaration

Kedar Sarmalkar
Greenhorn

Joined: May 25, 2007
Posts: 16
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

Joined: Apr 07, 2009
Posts: 489

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..


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Kedar Sarmalkar
Greenhorn

Joined: May 25, 2007
Posts: 16
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Kedar Sarmalkar
Greenhorn

Joined: May 25, 2007
Posts: 16
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

Joined: Oct 16, 2008
Posts: 952
Wow, something new to learn


SCJP 6
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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

Joined: May 25, 2007
Posts: 16
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: Oct 16, 2008
Posts: 952
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

Joined: May 25, 2007
Posts: 16
@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
 
subject: variable Declaration
 
Similar Threads
Can you explain this?
How would i break this down into seperate metods?
who can get the simplest code?
idont understand why i get this resolt
NullPointerException