Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1100
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: Trouble with updating reference variables through method calls |
| Date | 2011-09-22 22:47 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <j5gs27$6f2$1@dont-email.me> (permalink) |
| References | <3bf29bca-0e7e-4104-8b55-2e4589dd2ce9@x32g2000prf.googlegroups.com> |
On 9/22/2011 8:42 PM, Fred wrote:
> Hello,
>
> If this sort of code actually works (which I doubt), I might have to
> post some of my actual code for help.
>
> Why is foo1 updated in updateFoo but not in main() (if choice is 1)?
Nothing at all is updated, in any method, regardless of choice.
Reason: The code won't compile, much less run, not even if you fill
in the "..." bits with whatever you please.
> class Bar {
> private static void getNewFoo() {
> Foo foo = new Foo();
> return foo;
Returning a Foo from a void method? Yeah, right ...
> }
>
> private static void updateFoo(Foo foo1, Foo foo2, Foo foo3) {
> int choice = getChoice();
> ...
> if (choice == 1)
> foo1 = getNewFoo();
Assigning a value from a void method? Yeah, right ...
But if getNewFoo() somehow managed to return something, that
value would replace whatever was in the variable `foo1'. Note that
the variable `foo1' is local to the updateFoo() method, and has no
connection with whatever the method's caller provided as an argument.
It may sound picky, but to understand what's afoot you must distinguish
between *parameter* and *argument*:
- A *parameter* is a variable that is local to a method. Like
any other local variable, it is entirely owned by the method and
has no existence outside it. Any changes made to a parameter's
value are seen only within the method, just as with other local
variables declared inside the method.
- An *argument* is a value provided by the caller to establish
the initial value of the corresponding parameter. The argument
has no further connection with the parameter; all it does is set
the parameter's initial value. Changing the parameter has no
effect whatever on the argument. Indeed, the argument may be
something that cannot be changed, like the constant `null' or
the expression `x.getY("z")'.
(I've talked about method parameters here; for completeness I should
also have mentioned parameters of constructors and of catch blocks.
Completeness can go hang.)
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar
Trouble with updating reference variables through method calls Fred <albert.xtheunknown0@gmail.com> - 2011-09-22 17:42 -0700 Re: Trouble with updating reference variables through method calls Lew <lewbloch@gmail.com> - 2011-09-22 18:01 -0700 Re: Trouble with updating reference variables through method calls Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-22 22:47 -0400 Re: Trouble with updating reference variables through method calls Roedy Green <see_website@mindprod.com.invalid> - 2011-09-22 20:29 -0700 Re: Trouble with updating reference variables through method calls Roedy Green <see_website@mindprod.com.invalid> - 2011-09-22 20:33 -0700 Re: Trouble with updating reference variables through method calls Roedy Green <see_website@mindprod.com.invalid> - 2011-09-22 20:34 -0700 Re: Trouble with updating reference variables through method calls Travers Naran <tnaran@gmail.com> - 2011-09-22 21:27 -0700
csiph-web