GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes variable scope question/confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "variable scope question/confusion" Watch "variable scope question/confusion" New topic

variable scope question/confusion

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
The following code was taken from the book Java2: A Beginner's Guide by Herbert Schildt. After this object declaration:

Queue Q2 = new Queue(name); (about three quarters of the way down) there are two variables declared:

char ch;
int i;

In the "put" method, there is already a variable "ch" (void put(char ch)). What is the difference between the two? Are they the same variable or does one belong to the method and the other to the main method? Can you have two variables having the same name belonging to different blocks?

Here is the code:

Thank you,
S Keith

Joined: Feb 06, 2008
Posts: 4
Its all to do with the variables scope.

In java we can have the following types of variables:

instance variables
class variables
local variables

They have different scopes. Instance variables are 'alive' if the object they live in is alive.

Class variables are alive when the class is loaded.

Local variables are alive if the method they live in is still on the stack.

Parameters are local variables.

The variables you were referring to are local variables with the same names but in different methods, and different classes.

Theres more to this. Anyone have a link?
[ March 24, 2008: Message edited by: S Keith ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 39084
Welcome to the Ranchg, vonique. S Keith is correct about kinds of variables, and also that parameters behave rather like local variables. In a method with a parameter called "ch" the use of "ch" is implicitly the parameter called "ch"; if you want the field called "ch" use "". The local variables [or parameters] "shadow" the field (See the Java Language Specification �14.4.3.

Please use copy-and-paste to quote code rather than writing it out; I can see two compiler errors in what you quote; the means you have an extraneous ; in a for-loop declaration and you quote System.outprintln() which is missong a dot. I presume those are typing errors.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Thank you, I get it now (I think). Does anyone have any good suggestions as to how one would figure out complex code with incremeting values and variables, etc., how to follow the progression of loops and other data structures, for example like maybe with a flowchart or other method? I keep trying to figure out code from beginner Java books but on my own without anyone to explain it to me, it's so hard to untangle what seems like a mess of code that doesn't make sense.

If there is anyone who is a really good java programmer, can you think back to when you first started and tell me what was it that helped you to figure out what the code was supposed to do? I'm thinking also of taking an online java course but usually they just allow you to access some online tutorial and there isn't anyone on the other end to help you figure it out.

Anyone know of any online courses where there is an instructor who will actually answer an email with an answer?

Don't get me wrong---I usually can figure stuff out eventually, but it takes me a week to make sense of something that probably would only take an hour with some help.

Thank you!
Katrina Owen

Joined: Nov 03, 2006
Posts: 1362
Hi Vonique,

The three things that helped me immensely were
1) adding a bunch of println("note to myself") statements all throughout the code
2) buying and reading Head First Java by Kathy Sierra and Bert Bates
3) joining the Cattle Drive here at the Ranch.

The println() statements helped me see where the code was going, or what a variable was at a certain point, or whether or not the code actually entered a code block.

The Head First book was how I found the JavaRanch in the first place, and it told Java like a story - and a good story at that.

The Cattle Drive gives feedback - previously to that I had handed in assignments in College that were 1000s of lines long, and as long as they ran, nobody said anything about them. In the Cattle Drive I learned some of the basic things to look for to make my code simpler, easier to read, easier to write and a lot friendlier to other developers, and usually a lot more efficient as well.

Asking questions here is also a great place to get help unravelling bits and pieces of code.
jQuery in Action, 2nd edition
subject: variable scope question/confusion