All your code is doing is swapping the x and y variables in your method, not your classes variables. Try this.
As a suggestion you might want to read a little about variable scope. That's what was causing your confusion. In essence, you were creating two new x and y variables inside your switch method, and never changing the original variables.
Also methods should be named with a lowercase letter. [ July 14, 2005: Message edited by: Hentay Duke ]
Edit: Sorry my bad. I switched the case of method name in the post but not in my program. Switch is a terrible name for your own method (the compiler thinks you want to use a switch statement after it's changed to lowercase) change it to swap and you'll be all set. I've corrected it below
"switch" is a reserved word in Java, so it can't be a function name! Mr. Duke has gotten around this by using a capital "S", "Switch" -- a subtlety which "Mark Jim Tony" missed. A better solution would be to use the more traditional name "swap" for this function.
Now, "Mark Jim Tony" --
You may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. No fake-sounding names. You can change your display name here. Thanks!
The problem is that "switch" is a keyword in Java -- you can't create a function called "switch". Hentay's worked because he called it "Switch" with a capital "S". The problem here is that Java convention calls for methods to start with a lower-case letter. I'd call it "swap" or "switchIntegers" or some other name.
Yes in my original post when I went to copy my code into the post I noticed that he had named his method Switch and just changed it to lowercase in the post without going back and changing it in my IDE and re-running it. I totally missed the fact that switch would then cause errors(silly mistake on my part I just wasn't thinking). I apologize for any confusion.
I would like to suggest a few modifications to Hentay's solution. First of all, the tmp variable is unnecessary because you are using the parameters to swap the member variables. This means that you can do this instead:
However, this doesn't do a true swap because the new values of x and y can be totally unrelated to the old values. For example, what if we do something like this:
What is being "swapped" here? Nothing, really. The old values of x and y are completely replaced.
(As a side note, notice that you don't need to use the Integer wrapper class in Hentay's modified version of your code. You can just as easily use the primitive int type. However, the same issue still arises: this code does not actually swap the values of x and y unless you happen to call the method with the correct arguments.)
So how do you write a "real" swap() method in Java? Well, it's a little tricky becaus as you have probably already noticed, you can't use primitive types since their values are copied. Other problems occur when you try to use the wrapper classes. First of all, the wrapper classes are immutable. You cannot change the value stored inside of it. Second of all, references are passed-by-value, so just swapping the references doesn't do any good because, as you have already seen, it doesn't affect the original variables.
One possible solution is a slight variation of your original code. The main change I would make is completely remove the parameters altogether. This allows the method to access the class variables and swap them directly:
Once again, this doesn't require the use of the wrapper classes. You can do this just as easily with a primitive int.
Another solution is to write your own wrapper class that allows the caller to modify the internal data:
Now we can pass instances of this class to a swap method:
Notice that I didn't use the Integer wrapper class here because it isn't necessary, yet again.
In my opinion, none of these solutions is very elegant. However, I find that I rarely need to swap two values when I'm programming in Java. If I do, I will just write the code in-place rather than delegating it to another method.
I hope this helps, even though other people have already answered your original question.
Originally posted by Mark Jim Tony: Sorry ya.. The coding is not working. (snip...) [/CODE]
I think others have explained why you recieved these errors. I would like to point out that Hentay changed the name of your method because it is common convention to start method names with lower-case letters. We usually start class names with upper-case letters. If you adopt this convention, you will be able to easily recognize whether a certain name is a class or a method when you read your own code or another programmer's (assuming they also follow this convention).