Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: question about passing object by value (workaround)

  1. #1
    joet1984 is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default question about passing object by value (workaround)

    I have 3 classes. Gui, Piece, and Logic .. its a chess game in java 3d.
    The way it works is I have several objects with different characteristics. I have a Scene object, an ObjectFile, a TransformGroup, and a BranchGroup. Different classes are used to initialize these objects.

    Gui contains the branch group, Piece contains the objectFile, and TransformGroup, and Logic is used as a manager class for multiple pieces. In my Logic class, I have a HashMap<Integer screenPos, Piece piece> which is a collection of multiple piece objects and their position to be displayed on screen.

    Gui calls Logic, and logic runs a for loops for instance
    Java Code:
    HashMap<Integer, Piece> squares = new HashMap<Integer, Piece>();
    for(int i=0; i < 100; i++){
         squares.put(i, new(Piece(x,y,z));
    }
    for each piece an objectFile is loaded, some other irrelevant things happen, and then all the settings are added to a TransformGroup object.

    Now Gui looks something like this
    Java Code:
    BranchGroup bg = new BranchGroup();
    Logic logic = new Logic();
    for(TransformGroup t : logic.getTransformGroups()){
        bg.addChild(t);
    }
    SimpleUniverse.addBranchGroup(bg);
    Now for a little more explaining, logic.getTransofrmGroups() gets all the transforms groups for each piece by iterating through the hashmap and calling Piece.getTransformGroup() and stores it in an arraylist<TransformGroup>. Now when I go to load the program, the images do not show up. When this was all in one class, the images show up fine. So! It seems to me as if this is a pass by reference issue and I don't 100% understand how passing objects work in java. I have to be able to change the transformgroups later I believe in order for animation to occur (I think I'm not there yet). Am I thinking correctly? This is not working because these objects are being passed by reference and not value, right? If so, what would be a workaround for something like this? Any ideas? Thanks community :)

    Also, I am more than willing to post the actual code, I kind of just dumbed it down a little bit and wrote this off the top of my head in an attempt to make it easier to understand. Thanks!

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: question about passing object by value (workaround)

    Ok, unlike C/C++ you cannot pass a pointer. So, if you pass an object to a method, changes on THAT object apply when the method returns; however, if you change the reference inside that method to point to a different object, when the method returns it will still point to the old object. It's difficult to explain in words, so I'll show you.

    Java Code:
    public class References
    {
    	public static void main(String[] args)
    	{
    		SomeObject a = new SomeObject("A");
    		System.out.println("Inside main, before method call: " + a.getName());
    		// a holds SomeObject with name=A
    		someMethod(a);
    		// a still holds SomeObject with name=A, even if the reference was changed inside someMethod
    		// if a point was passed in C/C++ this would not work the same way
    		System.out.println("Inside main, after method call: " + a.getName());
    	}
    	
    	public static void someMethod(SomeObject o)
    	{
    		// o holds SomeObject with name=A
    		System.out.println("Inside someMethod, before assignment: " + o.getName());
    		o = new SomeObject("B");
    		// now o holds SOmeObject with name=B
    		System.out.println("Inside someMethod, after assignment: " + o.getName());
    	}
    }
    
    class SomeObject
    {
    	private String name;
    	
    	public SomeObject(String name)
    	{
    		setName(name);
    	}
    	
    	public void setName(String name)
    	{
    		this.name = name;
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    }
    The output of this program is:
    Inside main, before method call: A
    Inside someMethod, before assignment: A
    Inside someMethod, after assignment: B
    Inside main, after method call: A



    As for "ways around it", you could instead send a reference of an object containing the object itself (a wrapper of sorts), or call a method on the object to get a new object and assign it from the return result.

    Java Code:
    public class References
    {
    	public static void main(String[] args)
    	{
    		SomeObjectWrapper aWrap = new SomeObjectWrapper(new SomeObject("A"));
    		System.out.println("Inside main, before method call: " + aWrap.getObject().getName());
    		someMethod(aWrap);
    		System.out.println("Inside main, after method call: " + aWrap.getObject().getName());
    	}
    	
    	public static void someMethod(SomeObjectWrapper o)
    	{
    		System.out.println("Inside someMethod, before set: " + o.getObject().getName());
    		o.setObject(new SomeObject("B"));
    		System.out.println("Inside someMethod, after set: " + o.getObject().getName());
    	}
    }
    
    class SomeObjectWrapper
    {
    	private SomeObject o;
    	
    	public SomeObjectWrapper(SomeObject o)
    	{
    		setObject(o);
    	}
    	
    	public SomeObject getObject()
    	{
    		return o;
    	}
    	
    	public void setObject(SomeObject o)
    	{
    		this.o = o;
    	}
    }
    
    class SomeObject
    {
    	private String name;
    	
    	public SomeObject(String name)
    	{
    		setName(name);
    	}
    	
    	public void setName(String name)
    	{
    		this.name = name;
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    }
    The output is this:

    Inside main, before method call: A
    Inside someMethod, before set: A
    Inside someMethod, after set: B
    Inside main, after method call: B



    Another option would be to return the new Object like this:
    Java Code:
    public class References
    {
    	public static void main(String[] args)
    	{
    		SomeObject a = new SomeObject("A");
    		System.out.println("Inside main, before method call: " + a.getName());
    		a = someMethod(a);
    		System.out.println("Inside main, after method call: " + a.getName());
    	}
    	
    	public static SomeObject someMethod(SomeObject o)
    	{
    		System.out.println("Inside someMethod, before assignment: " + o.getName());
    		o = new SomeObject("B");
    		System.out.println("Inside someMethod, after assignment: " + o.getName());
    		return o;
    	}
    }
    
    class SomeObject
    {
    	private String name;
    	
    	public SomeObject(String name)
    	{
    		setName(name);
    	}
    	
    	public void setName(String name)
    	{
    		this.name = name;
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    }
    Output:
    Inside main, before method call: A
    Inside someMethod, before assignment: A
    Inside someMethod, after assignment: B
    Inside main, after method call: B
    Last edited by AndrewM16921; 02-27-2013 at 02:58 AM.

  3. #3
    joet1984 is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default Re: question about passing object by value (workaround)

    Awesome. Really thorough answer. Thanks for taking the time to do this for me!

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: question about passing object by value (workaround)

    Quote Originally Posted by joet1984 View Post
    Awesome. Really thorough answer. Thanks for taking the time to do this for me!
    Not a problem ^_^ Glad I could help

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: question about passing object by value (workaround)

    Just to clear up some of the loose terminology I see in this thread: pass by reference?

    To repeat: Objects are not passed at all.

    db
    PhHein likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Passing a Statement Object into a Program Object
    By Adomini in forum New To Java
    Replies: 2
    Last Post: 02-23-2013, 03:57 PM
  2. java-object passing by value
    By aumkar0406 in forum New To Java
    Replies: 15
    Last Post: 01-04-2012, 02:08 AM
  3. Replies: 1
    Last Post: 10-27-2011, 07:21 AM
  4. Is this even possible? Is there a workaround?
    By BoyWithACoin in forum Eclipse
    Replies: 0
    Last Post: 04-25-2009, 12:40 PM
  5. Passing SWT object to another class
    By Vinaya Lal Shrestha in forum SWT / JFace
    Replies: 0
    Last Post: 04-03-2009, 01:50 PM

Posting Permissions

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