This is my first Hello.java class.
public class Hello {
String name = "";
}
This is my second class Test1.java
public class Test1 {
public static void main(String[] args) {
Hello h = new Hello();
Test1 t = new Test1();
t.build(h);
System.out.println(h.name);
}
void build(Hello h){
h.name = "me";
}
}
When I run Test1.java, it prints "I". I think, I understand, because of the "transfer link".
This is my third class Test2.java
public class Test2 {
public static void main(String[] args) {
Hello h = null;
Test2 t = new Test2();
t.build(h);
System.out.println(h == null);
}
void build(Hello h){
h = new Hello();
}
}
When I run Test2.java, it prints "true", why? Does this “link passing” no longer exist? I'm confused.
source
share