package ex;
class Item{
String text = "hello";
}
class A {
Item item;
private A() {}
private static class LazyHolder {
public static final A INSTANCE = new A();
}
public static A getInstance() {
return LazyHolder.INSTANCE;
}
}
public class Main {
public static void main(String[] args) {
A a = A.getInstance();
Item n0 = a.item;
a.item = new Item();
Item n1 = a.item;
a.item.text = "world";
Item n2 = a.item;
if(n0 != null)
{System.out.println(n0.text);}
else{System.out.println("null");};
// This print "null"
System.out.println(n1.text);
// This print "world"
System.out.println(n2.text);
// This print "world"
}
}
Hello I'm a student studying java alone. And i have a question.
As you see, n1 & n2 are not null, n0 is null. And n1.text and n2.text both has "world".
So when I saw this result, i got a conclusion, but i don't know what i think is true.
This is my question: If some field have null, Does it mean that the filed has no pointer?
re-question:
Can i understand that n0 "has" pointer to null, n1 and n2 has pointer to Item type Instance?