making an argument null
Question posted by: Gangreen
(Member)
on
May 15th, 2008 01:44 PM
Let's say I have something like:
Code: ( text )
Object o; main{ o = new Object(); makeNull(o); system.out.println (o == null) // should be true } public void makeNull(Object o){ o = null }
I believe that when I pass the object to the function makeNull it passes a clone, and not the reference?
Still I need this functionality in a project of mine. I should be able to make an object null in a method, so that in the other parts of the program it's null too...
any ideas?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
|
|
May 15th, 2008 02:22 PM
# 2
|
Re: making an argument null
No, when passing an object to a function, you pass a reference.
|
|
May 15th, 2008 02:50 PM
# 3
|
Re: making an argument null
Quote:
Originally Posted by Laharl
No, when passing an object to a function, you pass a reference.
|
I sincerely hate it that Gosling named those pointers 'refererences'. Java passes
everything, yes everything by value and when we talk about objects we basically
always talk about pointers to objects (which we have to call 'references').
Pass by value means that a copy is made of the value to be passed. So if we
think we pass an object we pass a copy of the pointer to that object. And that
clarifies it all.
Think of an Object (or descendant thereof) variable as a remote control to an
actual TV set. We can have more than one remote control to one TV set and
remote controls are copied when we want to pass them as parameters to methods.
kind regards,
Jos
|
|
May 15th, 2008 03:23 PM
# 4
|
Re: making an argument null
yes I thought so, but I still need a workaround for this problem...
Code: ( text )
public class test { static Object o; public static void main(String[] args) { o = new Object(); makeNull(o); System.out.println(o == null); } public static void makeNull(Object o){ o = null; } }
if I execute this, I get false written out. How can I implement the makeNull method so it is will make the console say true?
thanx
|
|
May 15th, 2008 03:29 PM
# 5
|
Re: making an argument null
Quote:
Originally Posted by Gangreen
yes I thought so, but I still need a workaround for this problem...
Code: ( text )
public class test { static Object o; public static void main(String[] args) { o = new Object(); makeNull(o); System.out.println(o == null); } public static void makeNull(Object o){ o = null; } }
if I execute this, I get false written out. How can I implement the makeNull method so it is will make the console say true?
thanx
|
I don't want to disappoint you but you can't. You can't write a method that changes
the original parameter that was passed in; period.
kind regards,
Jos
|
|
May 15th, 2008 03:53 PM
# 6
|
Re: making an argument null
I'll explain my problem like it is in my project..maybe that would give you an idea..
I have this kind of collection, where I can store objects in, and retrieve them. What i want to do is to make a confinement, so that an object stored in that collection will not be accessable when it's in the collection, you can only access it by retrieving it, and then doing whatever you need to do.
so that's why I made an addItem method that should make the given object null (after adding a clone of it to the collection)...
that's what I need to make.
I could make each object have a boolean isStored... but that would give me a lot of work to rewrite things, and probably isn't the most elegant solution.
|
|
May 15th, 2008 04:06 PM
# 7
|
Re: making an argument null
Quote:
Originally Posted by Gangreen
I'll explain my problem like it is in my project..maybe that would give you an idea..
I have this kind of collection, where I can store objects in, and retrieve them. What i want to do is to make a confinement, so that an object stored in that collection will not be accessable when it's in the collection, you can only access it by retrieving it, and then doing whatever you need to do.
so that's why I made an addItem method that should make the given object null (after adding a clone of it to the collection)...
that's what I need to make.
I could make each object have a boolean isStored... but that would give me a lot of work to rewrite things, and probably isn't the most elegant solution.
|
Some other options:
1. Have the addItem method store a copy of the given object, not a reference to the original object.
2. Design the object to be immutable, this may eliminate your fears about referencing the object elsewhere, if it was because of mutation.
3. Just be careful that when an object is added, it is handed off to the collection, without a reference being maintained in the caller.
4. Have your addItem method construct the object to be added instead of being passed the object itself. This is a bit of a hack because the same concerns may swirl around the object's subparts and this makes it harder to add polymorphic objects.
Also note that even true call-by-reference it doesn't solve your problem:
Code: ( text )
void addItem(Component*& comp) { //C++-tyle syntax ... comp = null; } ... Component* comp = new Component(); Component* copy = comp; collection.addItem(comp); //now comp is null but copy still points to the object!
|
|
May 15th, 2008 08:23 PM
# 8
|
Re: making an argument null
well, this is java I have to work in. With C's pointers this would be easy...
I can hardly believe something like this has not been done before :s
|
|
May 15th, 2008 08:41 PM
# 9
|
Re: making an argument null
Quote:
Originally Posted by Gangreen
well, this is java I have to work in. With C's pointers this would be easy...
I can hardly believe something like this has not been done before :s
|
Please read my reply again. It can't be done in C/C++. Rethink your problem.
|
|
May 16th, 2008 07:05 AM
# 10
|
Re: making an argument null
Quote:
Originally Posted by Gangreen
well, this is java I have to work in. With C's pointers this would be easy...
I can hardly believe something like this has not been done before :s
|
You can't do it in C either; you have to pass a pointer to the thing you want to
nullify, as in:
Code: ( text )
void nullify(void** p) { *p= null; }
You can do it in C++ because C++ supports the pass by reference mechanism:
Code: ( text )
void nullify(void*& p) { p= null; }
But there is no way to do it in Java like this.
kind regards,
Jos
|
|
May 16th, 2008 06:08 PM
# 11
|
Re: making an argument null
Quote:
Originally Posted by JosAH
You can do it in C++ because C++ supports the pass by reference mechanism:
Code: ( text )
void nullify(void*& p) { p= null; }
But there is no way to do it in Java like this.
kind regards,
Jos
|
We have to define "it" before we can conclude whether "it" can be done. My understanding is that the OP wanted to add an object to a collection and ensure that the caller no longer had a direct reference to the object -- the caller would have to use the collection to get tot he object.
The code quoted above nullifies the pointer passed by reference, but what if the caller had a second pointer referencing the object? We have to assume the code followed some standard where the caller didn't do that, at which point we might as well assume the caller just performs a "pass off" -- passes the object to the add method and doesn't use the reference variable again.
 |
Not the answer you were looking for? Post your question . . .
170,099 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).
|
|
|
Top Java Forum Contributors
|