0
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?

홍윤표
  • 31
  • 4

2 Answers2

2

If some field have null, Does it mean that the filed has no pointer?

No it doesn't because a null pointer is still a pointer - it's just a pointer that's assigned null.

Keep in mind that being assigned a null pointer is quite different than being completely uninitialized. For example, the following won't even compile:

public static void main(String []args){
     String s;
     System.out.println(s);
 }

The following will compile, and it'll literally print null:

public static void main(String []args){
     String s = null;
     System.out.println(s);
 }

Some similar examples of trying to use something assigned null may throw a NullPointerException, which is actually one of the most discussed exceptions on Stack Overflow.

  • Thanks for your answer. I understood why n0 didn't have Instance, and n1 & n2 did ! – 홍윤표 Jan 08 '20 at 05:42
  • For the curious, there's [this Q&A](https://stackoverflow.com/questions/32211593/difference-between-uninitialized-and-null-pointer) about the difference between an uninitialized pointer and a null pointer in C. Different language obviously, but it's still instructive to read about. – EJoshuaS - Stand with Ukraine Jan 08 '20 at 05:44
0

there is not pointer feature in JAVA, so if any object is not initialized with any value then reference to NULL

divyang4481
  • 1,584
  • 16
  • 32
  • [Not strictly true](https://stackoverflow.com/questions/1750106/how-can-i-use-pointers-in-java). This answer is misleading at best, and is arguably flat-out wrong. – EJoshuaS - Stand with Ukraine Jan 08 '20 at 05:30
  • 1
    Weeellll...technically all non-primitive references are "pointers", it's just that you don't have direct access to the pointer it self, only what it points to, so you can modify the memory address of the reference (without assigning a new object to it) – MadProgrammer Jan 08 '20 at 05:32