This == null in java, after that also execution why continues?

This is my code. I set null for this link and then why it prints a non-null set

Test.java

try { new Test().setNull(); System.out.println("not null set"); } catch (Exception e) {//Catch exception if any System.err.println("Error: " + e.getMessage()); } } public void setNull() { setNull(this); } public void setNull(Object thisRef) { thisRef = null; } 

conclusion: not zero dialing

+4
source share
4 answers
  • Your code is incomplete. Next time send a minimal working example.

  • You pass this as a parameter to setNull() . Keep in mind that arguments are passed by value in Java. thisRef initialized to point to the same instance as this, but when you reassign null to thisRef , the value of this remains unchanged.

+1
source

Java is not a call by reference. Java is not a call by reference. Java is not ..

Assigning a local variable does not change any argument provided by the caller. Someday.

The function therefore does nothing: it assigns null local variable (that is, also to the parameter) and returns.

+11
source

Setting a reference to null will not throw NPE , otherwise think about how you would ever invalidate your references ? Also, when you null any link, only that link is separated from the object it points to. But the remaining links will still be there.

For example, for example:

 MyClass obj1 = new MyClass(); MyClass obj2 = obj1; obj1 = null; // only obj1 is detached from object System.out.println(obj2); // obj2 still points to original object 

So, when you call your method: -

 new Test().setNull(); 

A copy of the link is stored in this (Java does not follow the link , rather, it passes the links by value ), which is then passed to another method again, so another copy is created, which is then set to null. But the original link still points to this object.

NPE can be reset only when trying to call any method or access any properties of an object in a null reference.

+4
source

Another explanation of your own example:

 package com.test; public class Test { public void setNull() { System.out.println("Before setting null : "+ this); System.out.println("Going to set null"); this.setNull(this); System.out.println("'this' after setting null in caller method : "+this); this.print();// make sure that 'this' is not null; } public void print() { System.out.println("Another method"); } public void setNull(Object thisRef) { // here you are referring the 'this' object via a variable 'thisRef' System.out.println("Inside setNull"); System.out.println("thisRef value : " + thisRef); // same as 'this' // nullifying the 'thisRef' thisRef = null; System.out.println("thisRef after nullifying : "+thisRef);// ofcourse 'thisRef' is null System.out.println("'this' after setting null in method : "+this); // here the 'this' object will not be null } public static void main(String[] args) { new Test().setNull(); } } 

and console output:

 Before setting null : com.test.Test@3e25a5 Going to set null Inside setNull thisRef value : com.test.Test@3e25a5 thisRef after nullifying : null 'this' after setting null in method : com.test.Test@3e25a5 'this' after setting null in caller method : com.test.Test@3e25a5 Another method 
+1
source

Source: https://habr.com/ru/post/1447407/


All Articles