class ObjInstantiation {
public static void main(String[] args) {
new Greet().showGreeting(); //Nameless object
Greet g = new Greet();
g = new Greet(); //implicit deref.
g.showGreeting();
g = null; //explicit deref.
g.showGreeting(); // NullPointerException
}
}
class Greet {
void showGreeting() {
System.out.println("Hello World!");
}
}
Note: Greet g;
- loads Greet.class
to the memory and creates a reference variable g
for class Greet.
g = new Greet()
- creates a new object of class Greet and assigns the object’s address to the reference variable g.