1

I was wondering what the difference between declared instantiated and un-instantiated reference variables is.

For example:

  1. I have a class called Rhino.
    I make a variable of Rhino by declaring it, but I don't instantiate it.
  2. Then I make another variable of Rhino by declaring it and I instantiate to a value of null.

What is the difference between these two?

I'm attempting to make a singly linked list.

sKhan
  • 9,694
  • 16
  • 55
  • 53
chromechris
  • 215
  • 4
  • 9
  • Welcome to Stack Overflow. Could you please post a relevant sample of what you have so far? I encourage you to take the [tour](http://stackoverflow.com/tour) and to visit the [help center](http://stackoverflow.com/help/how-to-ask) for guidance on asking good questions on this site. – McMath Feb 28 '16 at 04:23

1 Answers1

0

Here is an example that you can try to find out the answer yourself:

package general;

public class BasicInstantiation {
private String name;

BasicInstantiation(String name){
    this.name = name;
}

public void setName(String name){
    this.name = name;
}

public String getName() {
    return name;
}

public static void main(String[] args) {
    try {
        BasicInstantiation bi = null;
        bi.setName("Romeo");
    }
    catch (NullPointerException ex){
        ex.printStackTrace();
    }

    try {
        BasicInstantiation bi1 = new BasicInstantiation("Romeo");
        bi1 = null;
        bi1.getName();          
    }
    catch (NullPointerException ex){
        ex.printStackTrace();
    }

}

}

Output:

java.lang.NullPointerException
at general.BasicInstantiation.main(BasicInstantiation.java:21)
java.lang.NullPointerException
at general.BasicInstantiation.main(BasicInstantiation.java:30)
The Roy
  • 2,178
  • 1
  • 17
  • 33
  • Thanks! Haha, I was more wondering towards the difference in this concept: – chromechris Feb 28 '16 at 05:14
  • package general; public class MyExample { MyExample one; // What does this reference point to, an Object of my Example? MyExample twoNull = null; // Points to nothing but is a variable of type MyExample right? public static void main(String[] args) { } } – chromechris Feb 28 '16 at 05:14
  • MyExample three = new MyExample(); // What does this point to? – chromechris Feb 28 '16 at 05:15
  • Point originOne; This notifies the compiler that you will use originOne to refer to data whose type is Point. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. If you declare originOne like this, its value will be undetermined until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object. For that, you need to use the new operator, as described in the next section. You must assign an object to originOne before you use it in your code. Otherwise, you will get a compiler error. – The Roy Feb 28 '16 at 07:37
  • The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor. – The Roy Feb 28 '16 at 07:38
  • Droy, your awesome! That was an awsome explanation. Thanks so much! – chromechris Feb 28 '16 at 15:06