Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1107

Re: Trouble with updating reference variables through method calls

From Travers Naran <tnaran@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: Trouble with updating reference variables through method calls
Date 2011-09-22 21:27 -0700
Organization A noiseless patient Spider
Message-ID <j5h1s0$15p$1@dont-email.me> (permalink)
References <3bf29bca-0e7e-4104-8b55-2e4589dd2ce9@x32g2000prf.googlegroups.com>

Show all headers | View raw


Must... resist... snottiness...  failing... failing...

On 22/09/2011 5: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)?

Because VB has caused indelible damage to the programming part of your 
brain.  You are used to VB6 with call-by-reference (aka pass by 
reference), no?[1]

Java uses pass by value so assigning a value to foo1 is assigning it to 
a new variable unrelated to foo1 in main().

>      private static void updateFoo(Foo foo1, Foo foo2, Foo foo3) {
>          int choice = getChoice();
>          ...
>          if (choice == 1)
>              foo1 = getNewFoo();
>          ...
>      }
>
>      public static void main(String[] args) {
>          Foo foo1 = new Foo();
>          Foo foo2 = new Foo();
>          Foo foo3 = new Foo();
>          updateFoo(foo1, foo2, foo3);
>      }


[1] Personally, I blame FORTRAN. :-)  Although it could have been worse.[2]

[2] VB could have used Call-by-name. :-)

Back to comp.lang.java.help | Previous | NextPrevious in thread | Find similar


Thread

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