• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scope coding question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Working on the MIT OpenCourseware stuff, and hit a snag on the Scope lesson.



For some reason, Eclipse is claiming that I have a duplicate variable X and an undefined Y. Please correct me on this, but I thought that I could redefine x (x = 6) and define y (y =72) without any trouble. Is there something in the syntax I have done wrong?

Thanks in advance.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Avery!

Welcome to JavaRanch!

Please try to remember to UseCodeTags. It makes your code easier to read.... and will get you more responses!
Cause you're new here, I added them for you
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not the syntax.... there's a concept here you're missing.

I'll bet on line 11 you're getting a compiler error. If you stop outputting y (or try to output on 2 lines), x's value will be 5 at line 11.

Why? Scope. Not the mouthwash.

Anything that gets declared INSIDE something (method, loop, etc) falls out of scope (and dies) once that something goes away.

Given that hint, let's see if you can fix it.... post back more code.


.... what? you didn't think I'd make it easy?

EDIT: and you should start off learning without Eclipse. You'll find it easier, retain more, and really understand what you're doing.... just my 2 cents.
 
Avery Jenkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:It's not the syntax.... there's a concept here you're missing.

I'll bet on line 11 you're getting a compiler error. If you stop outputting y (or try to output on 2 lines), x's value will be 5 at line 11.

Why? Scope. Not the mouthwash.

Anything that gets declared INSIDE something (method, loop, etc) falls out of scope (and dies) once that something goes away.

Given that hint, let's see if you can fix it.... post back more code.


.... what? you didn't think I'd make it easy?

EDIT: and you should start off learning without Eclipse. You'll find it easier, retain more, and really understand what you're doing.... just my 2 cents.



I wouldn't dream of you making it easy for me. I'd rather figure it out!

Actually, removing the y outputting entirely doesn't provide an answer for x. It claims I have a "Duplicate local variable x" on lines 6 and 7. I'll keep playing with the code and see what I figure out.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhh.... a duplicate local variable.... what could that possibly mean?

More than one of the same variable inside some scope, somewhere..... sounds like a problem.
After all, if there's two people with the same name, how would you tell them apart?
 
Avery Jenkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:Ahhh.... a duplicate local variable.... what could that possibly mean?

More than one of the same variable inside some scope, somewhere..... sounds like a problem.
After all, if there's two people with the same name, how would you tell them apart?



You're going to be fun to work with, I can tell.

I can see where that would be a problem, however, can't you set and reset variables as you go through a script?

EDIT: I just thought about that, and that would be silly. I think I'll go work a bit before I ask some more questions.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can change variables as much as you want (as long as they're not marked Final!)

Here's the issue: you're trying to CREATE two variables with the same name.

Imagine:


If I had 2 dogs named leroy, how could they possibly know which one was in trouble if I was hollering, "LEROY!!!"
Very confusing. The compiler thinks so, too.
 
Avery Jenkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I think I have it.


Let me see if I learned anything from this... ok, declaring variables inside an if statement... bad. I cannot declare x more than once in the same scope (which is defined as the area within {}. I could declare x again, but I would have to make sure it was in a new set of {}. Is that accurate?

Oh, and the answers I came up with were:

x = 5y = 72
x = 5y = 72

Oh, and before I forget, thanks Janeice!
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Avery Jenkins wrote:
You're going to be fun to work with, I can tell.



That's why I like it here. I have fun, you have fun, we learn stuff.... good times!

 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, now we can talk about why stuff works.

The y works on the top there, because once you get after the loop, the scope of inside the loop ends. You can also:



You can change x as many times as you'd like. You could fix the program by getting rid of the initialization:
 
reply
    Bookmark Topic Watch Topic
  • New Topic