Java- Object Instantiation

class ObjInstantiation {
public static void main(String[] args) {

new Greet().showGreeting();//Nameless object

Greet g = new Greet();
g = new Greet();//implicit deref.
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.
creates a reference variable (g) for class Greet.
g = new Greet() - creates a new object of class Greet.
assigns the object’s address to the reference variable g.

Post a Comment