I tired the following Java code and the result was "null". I thought two n are same pointer pointing to different memory (in main function and in test function). If I initiate n as null in main function, assign it in test function and want to use it outside test function (without return n itself). How can I do it?
public class ttt {
static class LinkNode {
int val;
public LinkNode(int p) {
val = p;
}
}
public static void test(Set<LinkNode> n) {
n = new HashSet<>();
LinkNode a = new LinkNode(1);
n.add(a);
}
public static void main(String[] args) {
Set<LinkNode> n = null;
test(n);
System.out.println(n);
}
}