473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

making an argument null

Let's say I have something like:

Expand|Select|Wrap|Line Numbers
  1. Object o;
  2.  
  3. main{
  4.       o = new Object();
  5.       makeNull(o);
  6.       system.out.println (o == null) // should be true
  7. }
  8.  
  9. public void makeNull(Object o){
  10.       o = null
  11. }
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?
May 15 '08 #1
10 1728
Laharl
849 Expert 512MB
No, when passing an object to a function, you pass a reference.
May 15 '08 #2
JosAH
11,448 Expert 8TB
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 15 '08 #3
yes I thought so, but I still need a workaround for this problem...

Expand|Select|Wrap|Line Numbers
  1. public class test {
  2.     static Object o;
  3.  
  4.     public static void main(String[] args) {
  5.         o = new Object();
  6.         makeNull(o);
  7.         System.out.println(o == null);
  8.  
  9.     }
  10.  
  11.     public static void makeNull(Object o){
  12.             o = null;
  13.     }
  14. }
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 15 '08 #4
JosAH
11,448 Expert 8TB
yes I thought so, but I still need a workaround for this problem...

Expand|Select|Wrap|Line Numbers
  1. public class test {
  2.     static Object o;
  3.  
  4.     public static void main(String[] args) {
  5.         o = new Object();
  6.         makeNull(o);
  7.         System.out.println(o == null);
  8.  
  9.     }
  10.  
  11.     public static void makeNull(Object o){
  12.             o = null;
  13.     }
  14. }
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 15 '08 #5
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 15 '08 #6
BigDaddyLH
1,216 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1. void addItem(Component*& comp) { //C++-tyle syntax
  2.     ...
  3.     comp = null;
  4. }
  5.  
  6. ...
  7. Component* comp = new Component();
  8. Component* copy = comp;
  9. collection.addItem(comp);
  10. //now comp is null but copy still points to the object!
May 15 '08 #7
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 15 '08 #8
BigDaddyLH
1,216 Expert 1GB
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 15 '08 #9
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. void nullify(void** p) { *p= null; }
  2.  
You can do it in C++ because C++ supports the pass by reference mechanism:

Expand|Select|Wrap|Line Numbers
  1. void nullify(void*& p) { p= null; }
  2.  
But there is no way to do it in Java like this.

kind regards,

Jos
May 16 '08 #10
BigDaddyLH
1,216 Expert 1GB
You can do it in C++ because C++ supports the pass by reference mechanism:

Expand|Select|Wrap|Line Numbers
  1. void nullify(void*& p) { p= null; }
  2.  
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.
May 16 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

10
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...
5
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...
351
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...
4
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...
3
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...
11
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...
10
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...
8
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 ...
4
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
1
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)...
0
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
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.