Results 1 to 2 of 2
  1. #1
    Himetic is offline Member
    Join Date
    Feb 2012
    Rep Power

    Default Variable argument lengths passing by reference issues

    I'm making an android game and I've created a button class, which my various buttons can all be subclasses of, and each implements the abstract "onPress" method. Since the onPress will do a variety of things depending on the button, I figured the generic Object... parameter made the most sense. But when I went to create my first button, which simply inverts a boolean when pressed, it doesn't seem to take.

    in the main class:

    boolean turretMenuOpen = false;
    Turret_menu_button button = new Turret_menu_button;

    in the superclass:

    public abstract void onPress(Object... objects);

    in the subclass:

    public void onPress(Object... objects) {
    objects[0] = !((Boolean)objects[0]).booleanValue();

    I can see that I'm getting the boolean passed ok, it's false when it's in the onPress method. But my assignment doesn't ever actually change the value of the variable in main. Even if I just assign it directly to true, it's still false in main. Why isn't it sticking? I thought it might be a reference/value thing, but it seems like directly assigning it a value should definitely change the object it's pointing to. I even tried changing it to the nonprimitive Boolean, but it didn't make any difference. Halp!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Variable argument lengths passing by reference issues

    Under the hood the compiler is doing:
    Java Code:
    boolean turretMenuOpen = false;
    Boolean[] temparray = new Boolean[1]();
    temparray[0] = turretMenuOpen;
    That array is passed into the method call.
    Now, if you wrote that directly into code as follows:
    Java Code:
    public class Scratch {
        * @param args
       public static void main(String[] args) {
          boolean b = true;
          Boolean[] temparray = new Boolean[] {b};
          temparray[0] = Boolean.FALSE; // This represents the method call
    You'll see that 'b' doesn't change, it is still true.
    Which is as expected.
    The only thing that was changed was the reference inside the temporary array, which is not reflected in 'b'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Question on Passing by Reference..
    By fatabass in forum New To Java
    Replies: 1
    Last Post: 02-07-2012, 12:02 AM
  2. change variable given as argument to function
    By Danieljabailey in forum New To Java
    Replies: 4
    Last Post: 07-27-2011, 11:48 PM
  3. Passing method as argument
    By susieferrari in forum New To Java
    Replies: 26
    Last Post: 06-13-2011, 10:43 AM
  4. Reference variable
    By lala in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 07:27 PM
  5. Passing Class Reference to method
    By nekt in forum Advanced Java
    Replies: 5
    Last Post: 03-26-2009, 05:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts