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?
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.
Joined: Mar 24, 2008
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.
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.