Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.help Subject: Re: Trouble with updating reference variables through method calls Date: Thu, 22 Sep 2011 22:47:40 -0400 Organization: A noiseless patient Spider Lines: 57 Message-ID: References: <3bf29bca-0e7e-4104-8b55-2e4589dd2ce9@x32g2000prf.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 23 Sep 2011 02:48:39 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="f8igmItKsWs6nM5YanFxAA"; logging-data="6626"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+2ZwHnFMrOIiHAt+v5phAE" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20110902 Thunderbird/6.0.2 In-Reply-To: <3bf29bca-0e7e-4104-8b55-2e4589dd2ce9@x32g2000prf.googlegroups.com> Cancel-Lock: sha1:FaR+giDrBxTq2OGGC+TsqD8vSMc= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:1100 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