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?

3
  • en.wikipedia.org/wiki/Null_pointer Commented Jan 8, 2020 at 5:26
  • 1
    @Fenio Actually, the answer is "no" because a null pointer is still a pointer. Commented Jan 8, 2020 at 5:34
  • As @EJoshuaS-ReinstateMonica already said, it is still a pointer but this pointer points to null. So your field has a pointer but that points to nothing. Commented Jan 8, 2020 at 5:36

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. I understood why n0 didn't have Instance, and n1 & n2 did !
For the curious, there's this Q&A about the difference between an uninitialized pointer and a null pointer in C. Different language obviously, but it's still instructive to read about.
0

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

2 Comments

Not strictly true. This answer is misleading at best, and is arguably flat-out wrong.
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.