Sunday, December 19, 2010

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.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.
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.

0 comments:

Post a Comment

Contact Form

Name

Email *

Message *

The Insane Techie - Android App

Launched an android app for the blog on 07th June 2016. Get it from google play store... Tips for using the app Use in landscape mo...