Let's say I have something like: -
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?
10 1728
No, when passing an object to a function, you pass a reference.
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
yes I thought so, but I still need a workaround for this problem... - 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
yes I thought so, but I still need a workaround for this problem... - 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
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.
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: - 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!
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
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.
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: -
void nullify(void** p) { *p= null; }
-
You can do it in C++ because C++ supports the pass by reference mechanism: -
void nullify(void*& p) { p= null; }
-
But there is no way to do it in Java like this.
kind regards,
Jos
You can do it in C++ because C++ supports the pass by reference mechanism: -
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David Murmann |
last post by:
Hi all!
I could not find out whether this has been proposed before (there are
too many discussion on join as a sequence method with different
semantics). So, i propose a generalized .join method...
|
by: Jonathan Burd |
last post by:
Greetings everyone,
I wrote a function to learn about variable-length argument lists. I
wonder if there is a better way to detect the end of the argument list
than using a sentinel value like...
|
by: CBFalconer |
last post by:
We often find hidden, and totally unnecessary, assumptions being
made in code. The following leans heavily on one particular
example, which happens to be in C. However similar things can (and...
|
by: Patient Guy |
last post by:
Does anyone have any coding rules they follow when doing argument checking?
When arguments fail during check, do you return from the call with an
ambiguous return value, or do you throw...
|
by: matko |
last post by:
This is a long one, so I'll summarize:
1. What are your opinions on raising an exception within the
constructor of a (custom) exception?
2. How do -you- validate arguments in your own...
|
by: zefciu |
last post by:
I am trying to embed a c function in my python script for a first time.
When I try to call it I get an error
SystemError: new style getargs format but argument is not a tuple
Guido said on...
|
by: JurgenvonOerthel |
last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and
Derived2 derive publicly from Base.
Given a 'const Base &input' I want to initialize a 'const Derived1
&output'.
If the...
|
by: A. Anderson |
last post by:
Howdy everyone,
I'm experiencing a problem with a program that I'm developing. Take a
look at this stack report from GDB -
#0 0xb7d782a3 in strlen () from /lib/tls/i686/cmov/libc.so.6
#1 ...
|
by: istillshine |
last post by:
I have a function foo, shown below. Is it a good idea to test each
argument against my assumption? I think it is safer. However, I
notice that people usually don't test the validity of...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |