wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Scope coding question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scope coding question" Watch "Scope coding question" New topic
Author

Scope coding question

Avery Jenkins
Greenhorn

Joined: Jan 11, 2011
Posts: 5
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.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

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


When you do things right, people won't be sure you've done anything at all.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

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

Joined: Jan 11, 2011
Posts: 5
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

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

Joined: Jan 11, 2011
Posts: 5
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

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

Joined: Jan 11, 2011
Posts: 5
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

Avery [surl='http://jenkins-ci.org/' class='api' title='Continuous Integration Engine wrote:Jenkins[/surl]]
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scope coding question