Java memory allocation on stack vs heap -


I feel like a novice to ask this question - but why is it that when I set my method down Point down and point it to a new hashet, does it still come in the form of EmptySet? Is it because the local variable is allocated on the stack, and so when I exit my method my new is blown away? What can I get a functional equivalent?

  import java.util.HashSet; Import java.util.Set; Public category test method {public static zero main (final string [] args) {last set < Integer & gt; Foo = java.util.Collections.emptySet (); Test (foo); } Public static zero testing (set  mySet) {mySet = new hashset & lt; Integer & gt; (); }}  

Reference passes through Java value, mySet foo as a copy of the reference, in the zero test (set mySet) , mySet The variable is only a local variable within that function, so setting it in something else does not affect the caller main .

mySet sets the reference (or "point" if you want) as the foo variable in main Though.

If you want to change the context in the main, you can do this:

  foo = test (); // foo is not the last yet, although the public is set to set & lt; Integer & gt; Test () {new hashasset back & lt; Integer & gt; (); }  

Comments