making an argument null

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gangreen
    New Member
    • Feb 2008
    • 98

    making an argument null

    Let's say I have something like:

    Code:
    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?
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    No, when passing an object to a function, you pass a reference.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      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

      Comment

      • Gangreen
        New Member
        • Feb 2008
        • 98

        #4
        yes I thought so, but I still need a workaround for this problem...

        Code:
        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

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Gangreen
          yes I thought so, but I still need a workaround for this problem...

          Code:
          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

          Comment

          • Gangreen
            New Member
            • Feb 2008
            • 98

            #6
            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.

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              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=Java]void addItem(Compone nt*& comp) { //C++-tyle syntax
              ...
              comp = null;
              }

              ...
              Component* comp = new Component();
              Component* copy = comp;
              collection.addI tem(comp);
              //now comp is null but copy still points to the object![/CODE]

              Comment

              • Gangreen
                New Member
                • Feb 2008
                • 98

                #8
                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

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  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.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    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=c]
                    void nullify(void** p) { *p= null; }
                    [/code]

                    You can do it in C++ because C++ supports the pass by reference mechanism:

                    [code=c++]
                    void nullify(void*& p) { p= null; }
                    [/code]

                    But there is no way to do it in Java like this.

                    kind regards,

                    Jos

                    Comment

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      Originally posted by JosAH
                      You can do it in C++ because C++ supports the pass by reference mechanism:

                      [code=c++]
                      void nullify(void*& p) { p= null; }
                      [/code]

                      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.

                      Comment

                      Working...