• Post Reply Bookmark Topic Watch Topic
  • New Topic

quick question about using the same variable name in two classes  RSS feed

 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may says really dumb, but here goes:

Regarding the code examples in Head First Java, this is from Chapter 5, regarding the beginning creation of the dot com game. There are two classes in quesiton

the first is the SimpleDotComTester class:



and the second one is the code for the checkYourself () method in the SimpleDotCom class



Now I noticed that both classes use a variable called result; the program runs fine, but assume from the example that you can use the same variable name two different classes; why would you do that? How come Java doesn't get them confused? I thought it was a but confusing when I read through the examples myself. couldn't one of them be given another name?

 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following post explains it better than I did.
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any variable that is declared inside of a method is only visible within that method. As soon as that method finishes execution, those variable are wiped out and no longer exist.

Even if you have say 3 methods in the same class, each of the methods could declare a variable with the same name and there would still be no error because the variables declared inside the method only live inside that method and are not reachable from anywhere else in the class. You can pass the values out of the method by returning it but then the actual variable that held that value prior to the return would be wiped out.

Hope that helps a little. Hope I didn't confuse you more.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote:Now I noticed that both classes use a variable called result; the program runs fine, but assume from the example that you can use the same variable name two different classes; why would you do that?

Simple answer: Because they do the same thing, and there's only a limited number of meaningful names you can use.

For example: How many names can you think of that mean 'name' or 'description'? Now think of how many classes you might create that need a variable that contains exactly that sort of information.

How come Java doesn't get them confused?

Because Java has strict rules about variable scope, which make it well-nigh impossible for the compiler to make a mistake. Oddly, the Java Tutorials don't seem to mention it much (at least I can't find it); but I found a couple of pages here and here that may help to explain it for you.

A few general rules that might help:
1. There's absolutely nothing wrong with having variables with the same name, providing their scopes are different.
2. Make ALL member variables private. That way, you can't possibly confuse their scope, because they will never be visible outside the class.
3. Define variables as close as possible to where they're used.
The classic case is indexes: If you have an index that is only used in a loop, define it IN the loop. ie:
for (int x = 0; x < array.length; x++) { ...
NOT:
int x;
for (x = 0; x < array.length; x++) {
...

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are however several problems you can get with variables with the same name: shadowing and obscuring. Those links may be difficult to understand, but the code examples are reasonably simple.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is a quite common thing. especially with two seperate classes.
for example
amount = 100;
otherClass.method(amount);

otherClass
int amount;
method(int amount)
{
this.amount = amount;
}


as long as it does not cause confusion it is ok.

 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There are however several problems you can get with variables with the same name: shadowing and obscuring. Those links may be difficult to understand, but the code examples are reasonably simple.


It is definitely a value add CR. Hope you had been rocking as usual in Ranch.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you about that. Yes, still rockin'
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!