Sunday, July 13, 2014

Object declaration and creation in Java

Object declaration creation Initialization 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 variable and a real baby to an object.

The left box in figure represents variable declaration, because the baby hasn’t been born yet. The right box in figure represents object creation.

Object creation can be understood with these 3 steps:

Declaration of an object
Declarations can appear as part of object creation as you saw above
Person person;
Declarations simply notify the compiler that you will be using name to refer to a variable whose type is type. Declarations do not instantiate objects. To instantiate a Person  object, or any other object, use the new operator.

Instantiation of an object

The new operator instantiates a new object by allocating memory for it. new requires a single argument: a constructor method for the object to be created. The constructor method is responsible for initializing the new object.

Initialization of an object
Classes provide constructor methods to initialize a new object of that type. In out case, default constructor or Person class do the initialization of object
Person()
A constructor such as the one shown, that takes no arguments, is known as the default constructor. Like Person, most classes have at least one constructor, the default constructor. However, classes can have multiple constructors, all with the same name but with a different number or type of arguments.



If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...

3 comments:

  1. Pretty good explanation. Can you, please, make a post about comparing Objects in memory? The diferrences between using the .equals method and the == operator with objects? Thanks for the great job and sorry about the bad english.

    Ty :)

    ReplyDelete
    Replies
    1. Thanks Daniel for reading this post. Post on hashCode() and equal() method is already available on my blog. Here is the link
      java-latte.blogspot.in/2013/11/object-equality-in-java.html

      Delete
  2. Honestly, a never better explanation. Thank you sooo much

    ReplyDelete