Object references are passed by value in Java, so assigning a local variable within a method does not change the original variable. Only the local variable s points to a new line. This may be easier to understand with a little ASCII art.
You initially have this:
------------ | nullTest | ------------ | null
When you first enter the setNotNull method, you get a copy of the nullTest value in s . In this case, the nullTest value is an empty reference:
------------ ------------ | nullTest | | s | ------------ ------------ | | null null
Then rewrite s:
------------ ------------ | nullTest | | s | ------------ ------------ | | null "not null!"
And then leave the method:
------------ | nullTest | ------------ | null
Mark byers
source share