Object declaration and creation in Java

Unlike some other programming languages, such as C, Java doesn’t allow you to allocate or deallocate memory yourself when you create or destroy objects. Java manages memory for allocating objects and reclaiming the memory occupied by unused objects. The task of reclaiming unused memory is taken care of by Java’s garbage collector, which is a low-priority thread. It runs periodically and frees up space occupied by unused objects. In this post, we’ll see the terms Declaration, Instantiations and Initialization of an object.

Object is born
An object comes into the picture when you use the keyword operator new. You can initialize a reference variable with this object. Note the difference between declaring a variable and initializing it. The following is an example of a class Person and another class ObjectLifeCycle:

In the previous code, no objects of class Person are created in the class ObjectLifeCycle; it declares only a variable of type Person. An object is created when a reference variable is initialized:

The difference in variable declaration and object creation can be easy understood with this image. where you can compare a baby name to a reference variableand a real baby to an object.

Click here to read more detail 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s