Wednesday, 29 April 2015

Object in Java

What is an object

In object-oriented terms object is an instance of the class, which gets its state and related behavior from the class. As explained in the post (Class in Java) when a new class is created essentially a new data type is created. This type can be used to declare objects of that type.
An object stores its state in fields and exposes its behavior through methods.

Creating Objects

Creation of an object for a class is done in three steps -
  • Declaration - Declare a variable of the class type, note that no object is defined yet. This variable can refer to an object as and when the object is created.
  • Instantiation - Create an object and assign it to the variable created in step 1. This object creation is done using new operator.
  • Initialization - Class name followed by parenthesis after the new operator (new classname()) means calling the constructor of the class to initialize the object.
As Exp -
Rectangle rectangle;
This statement declares an object rectangle of the class Rectangle. It does not hold any thing yet.
rectangle = new Rectangle();
With this statement a new object rectangle is created, which means rectangle objects holds reference to the created object of Rectangle class.
Decalring an object in Java
Declaring an object of type Rectangle
As it can be seen from the figure the rectangle variable holds reference to the allocated Rectangle object.
The new operator allocates memory for an object at run time and returns a reference to that memory which is then assigned to the variable of the class type.
In new Rectangle(), the class name Rectangle() followed by parenthesis means that the constructor of the class Rectangle would be called to initialize the newly created object.

Refer to : Constructor in Java to know more about object initialization.
Generally this 3 step process is written in a single statement like -
Rectangle rectangle = new Rectangle();
This encapsulation of variable and methods into a class and using them through objects provides a number of benefits including -
  • Modularity - The application can be divided into different functional categories. Then code can be written for many independent objects implementing the specific functionality. Once created an object can be easily passed around inside the system creating a series of operations to execute functionality.
  • Information-hiding- By encapsulating the variables and methods and providing interaction to the outside entities only through object's methods, an object hides its internal implementation from the outside world.
  • Code re-use - While working on an application we use several third party libraries. That is an example how we use several already existing objects (already created and tested) in our application.
  • Pluggability and debugging ease - Since a whole application comprises of several working components in form of objects, we can simply remove one object and plug in another as replacement in case a particular object is not providing the desired functionality.

That's all for this topic Object in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related topics

  1. Class in Java
  2. this in Java
  3. finalize method in Java
  4. Object creation using new operator in Java
  5. Java OOP interview questions

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment