-
August 8th, 2010, 04:07 AM
#1
Assign new value to an object in a function
Hi,
I want to pass an object to another function (of another class) to assign new value to it: code below
Code:
public class MyClass
{
InfoSupplier infosupplier;
public void myFunc(){
Info infocarrier = new Info();
this.infosupplier.supply(infocarrier); //infosupplier has already been declared and allocated elsewhere
//the problem is after this line, infocarrier still doesn't have the new values (members)
//which means the assignment inside the function supply() is NOT effective
}
}
public class InfoSupplier
{
Info info;
public void supply (Info ic)
{
ic = this.info ;//this.info has been declared and allocated, assigned value elsewhere
}
}
What is wrong with the assignment? Given everything in Java is pointer
Thanks.
Last edited by choconlangthang; August 8th, 2010 at 04:08 AM.
Reason: missing the question :)
-
August 8th, 2010, 08:06 AM
#2
Re: Assign new value to an object in a function
Do you get any errors? Please post full text here.
Norm
-
August 8th, 2010, 10:05 AM
#3
Re: Assign new value to an object in a function
Originally Posted by Norm
Do you get any errors? Please post full text here.
Nope, there's no error, which cause a run-time bug for me.
Currently my workaround is that I assign all values (primitive type, not object type), however it is not what it SHOULD be, and lengthy.
[code ]
public void supply (Info ic)
{
ic = this.info ;//this.info has been declared and allocated, assigned value elsewhere
ic.member1.value = this.info.member1.value; //value is of primitive type, like int, boolean,...
//rest of code
}
[/code]
As of "full text", I suppose you mean my real code. It's just the same with some name changing.
-
August 8th, 2010, 10:09 AM
#4
Re: Assign new value to an object in a function
Not the code, The full text of the error message.
If there are no error messages, can you show what the values are by using println()s to display the variables when you set their values and when later you try to use them, to show what is wrong.
Code:
public void supply (Info ic)
{
ic = this.info ;
The ic used in this code is a local variable and it disappears (goes out of scope) when the method exits.
Change supply() to return a reference to an Info object and return this.info.
Last edited by Norm; August 8th, 2010 at 10:14 AM.
Norm
-
August 8th, 2010, 10:11 AM
#5
Re: Assign new value to an object in a function
-
August 8th, 2010, 10:15 AM
#6
Re: Assign new value to an object in a function
Go back and read the update to my last post
Norm
-
August 8th, 2010, 12:08 PM
#7
Re: Assign new value to an object in a function
What is wrong with the assignment? Given everything in Java is pointer
Read this article, it explains how Java passes parameters and why your code won't work.
-
August 8th, 2010, 04:02 PM
#8
Re: Assign new value to an object in a function
All parameters in Java are passed 'by value', which means that you can't pass back a changed 'top-level' reference - only modify properties of the object passed.
Here's how we handle converting from languages which allow pass by reference (C#, VB, and C++) to Java:
Code:
public class MyClass
{
private InfoSupplier infosupplier;
public final void myFunc()
{
Info infocarrier = new Info();
RefObject<Info> tempRef_infocarrier = new RefObject<Info>(infocarrier);
this.infosupplier.supply(tempRef_infocarrier);
infocarrier = tempRef_infocarrier.argvalue;
}
}
public class InfoSupplier
{
private Info info;
public final void supply(RefObject<Info> ic)
{
ic.argvalue = this.info;
}
}
//----------------------------------------------------------------------------------------
// Copyright © 2006 - 2010 Tangible Software Solutions Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
//
// This class is used to simulate the ability to pass arguments by reference in Java.
//----------------------------------------------------------------------------------------
public final class RefObject<T>
{
public T argvalue;
public RefObject(T refarg)
{
argvalue = refarg;
}
}
-
August 8th, 2010, 08:54 PM
#9
Re: Assign new value to an object in a function
@Norm: thanks but returning is not what I want, since I might need to return multiple values.
@keang & David: thanks for excellent article & code. So the trick is to wrap your object inside another object (like David's: T argvalue), making your original object sub-level.
-
August 9th, 2010, 07:10 AM
#10
Re: Assign new value to an object in a function
So the trick is to wrap your object inside another object (like David's: T argvalue), making your original object sub-level.
No, you should write your code in a manner is appropriate to the language you are using. David's code is, as he says, how they convert other languages that do pass by reference to Java. This 'trick' reduces the code changes required to get the non Java code ported to Java. I don't imagine they write new code like this.
In Java you would either change the contents of the object passed in to the method or you return the new object instance.
-
August 9th, 2010, 09:29 AM
#11
Re: Assign new value to an object in a function
That's absolutely right keang - converters just try to produce the equivalent code, which will often be different than the code you should be writing from scratch.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|