• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Lifetime and scope of a variable

 
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain this:

(a) for(int x=0;x<3;x++){int y=-1;} // it works


(b) for(int x=0;x<3;x++)
int y=-1;
case b does not work -- compile time error

(c) for(int x=0;x<3;x++);
int y=-1;

this also compiles

Why it does not allow a variable declaration without brackets?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is beacuse in the (b) option you are declaring int y inside the loop which is not allowed. You can declare like the following,

int y=1;
for(int i=1;<=5;i++)
y=1;
 
Nancy Kan
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, I got it, it will try to declare same var again and again on the same stack , hence error!
 
balajee annamalai
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then i have a doubt why then option A is working.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with you balajee
but first one is working because you put ({)curly braces.
its like a variable inside loop

but i did not understand if this is the case then why not

for(int i=0;i<4;i++)
int y=-1;
not working
see it means you have a loop with one variable declaration then what's the problem.

Seriously its confusing
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
i think in (b) the declaration statement is not permitted.I search in the

Java specification, but nothing is specified to explain this error.

I have another doubt


this statement works fine, but if i make the infinite loop, i have a cmpile
time error


So if anyone have an explanation,it be appreciated
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

this statement works fine, but if i make the infinite loop, i have a compile time error

as a general note, it is often quite helpful if you post the exact text of that compile time error.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int y=0; is declaration and its not considered as a statement according to the java language specification and since only statements are allowed after if or for construct, it gives a compiler error.
When you put {} it makes it as a block which is always allowed in these construct.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic