• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

variable scope question/confusion

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Vonique
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 80282
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 "this.ch". 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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic