How to Set an Object to Null

clearing or set null to objects in java

Firstly, you never set an object to null. That concept has no meaning. You can assign a value of null to a variable, but you need to distinguish between the concepts of "variable" and "object" very carefully. Once you do, your question will sort of answer itself :)

Now in terms of "shallow copy" vs "deep copy" - it's probably worth avoiding the term "shallow copy" here, as usually a shallow copy involves creating a new object, but just copying the fields of an existing object directly. A deep copy would take a copy of the objects referred to by those fields as well (for reference type fields). A simple assignment like this:

ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = list1;

... doesn't do either a shallow copy or a deep copy in that sense. It just copies the reference. After the code above, list1 and list2 are independent variables - they just happen to have the same values (references) at the moment. We could change the value of one of them, and it wouldn't affect the other:

list1 = null;
System.out.println(list2.size()); // Just prints 0

Now if instead of changing the variables, we make a change to the object that the variables' values refer to, that change will be visible via the other variable too:

list2.add("Foo");
System.out.println(list1.get(0)); // Prints Foo

So back to your original question - you never store actual objects in a map, list, array etc. You only ever store references. An object can only be garbage collected when there are no ways of "live" code reaching that object any more. So in this case:

List<String> list = new ArrayList<String>();
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put("Foo", list);
list = null;

... the ArrayList object still can't be garbage collected, because the Map has an entry which refers to it.

How to set all values of an object to null in JavaScript?

Here's a useful function called 'Object.keys()', it returns all of the attribute names of an object.

let setAll = (obj, val) => Object.keys(obj).forEach(k => obj[k] = val);
let setNull = obj => setAll(obj, null);

Non-arrow-function version:

function setAll(obj, val) {
/* Duplicated with @Maksim Kalmykov
for(index in obj) if(obj.hasOwnProperty(index))
obj[index] = val;
*/
Object.keys(obj).forEach(function(index) {
obj[index] = val
});
}
function setNull(obj) {
setAll(obj, null);
}

Set all references of object to null

That's not possible in Java.You can not set reference a and b null using bar.

Reason - Java is pass by value not pass by reference.

Is it possible to set an Object to be null from within itself?

No, because a is a reference (not an object as in this question's title) and no method can modify the value of a reference except the method in which it is defined (I assume from the code context that a is a local variable).

Since Java doesn't have pass-by-reference, what you ask cannot be done: there's no way to collect addresses-of references in order to manage the addresses pointed to. You might use a wrapper object, but not sure what'd be the point.

Set Javascript object values to null

fastest and most readable way in my opinion:

Object.keys(obj).forEach(key => obj[key]=null);

Object.keys retrieve all the keys from a given object, and then just iterate through to set them to null.

Assign an object to null

You can never assign to an object. All you ever have are primitives and references. A reference is either null or a pointer to an object of suitable class.

Java arguments are passed by value. Your called method got a copy of a reference. It made that reference null. The calling method has its own reference which was unaffected by any assignments to the passed copy. That reference still points to the object.

Set object to null, from list

As mentioned in the comments, this is just not how C# works. In particular, C# does not allow to store references to an object directly, only as part of another object. Hence, the following would work:

class Variable<T>
{
public Variable(T value) { Value = value; }
public T Value { get; set; }
}

var s = new Variable<string>("Test");
var list = new List<Variable<string>>();

list.Add(s);

Then you have A

s.Value = null;

Debug.Log( list[0].Value ); //Null
Debug.Log( s.Value ); //Null

and B

list[0].Value = null;

Debug.Log( list[0].Value ); //Null
Debug.Log( s.Value ); //Null

Does setting variable to null clear just the reference?

In your first example:

             +------+
obj -------> |OBJECT|
+------+

After setting obj to null:

             +------+
obj |OBJECT|
+------+

No reference to the object exists so it "doesn't exist" anymore because it has become unreachable.


In your second example:

obj -------> +------+
|OBJECT|
temp ------> +------+

After setting obj to null:

obj          +------+
|OBJECT|
temp ------> +------+

You can see that temp still references the object so it still "exists".

Cannot set an object to null inside a method

This is because parameters are passed by value, so in your test method, a copy of parent variable has been created, you are just setting the copy of variable to null. Though you still can manipulate the object because the copy variable is pointing to the object in the heap.

class Program
{
static void Main(string[] args)
{
Foo foo = new Foo();
SetNull(foo);

Console.WriteLine(foo.ID);// print 2
}

private static void SetNull(Foo foo)
{
foo.ID = 2;
foo = null;
}
}

class Foo
{
public int ID { get; set; }
}


Related Topics



Leave a reply



Submit