Monday, May 10, 2021

Object in Java

In object-oriented terms object is an instance of the class, which gets its state and related behavior from the class. An object stores its state in fields and exposes its behavior through methods.

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.

Creating object in Java

Creation of an object for a class in Java 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.

Java object creation example

If we have a class called Rectangle as this-

class Rectangle{
  double length;
  double width;
  
  //methods 
  ……
  ……
}

Then declaring object of the class Rectangle would be done as-

Rectangle rectangle;

This statement declares an object rectangle of the class Rectangle. It does not hold any thing yet.

With the following statement a new object rectangle is created (instantiated), which means rectangle objects holds reference to the created object of Rectangle class.

rectangle = new Rectangle();
Declaring an object in Java
Declaring and Instantiating 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 the statement 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. This step is the initialization step.

Generally this three step process of declaring, creating and initializing an object in Java is written in a single statement as following-

Rectangle rectangle = new Rectangle();

Benefits of using class and object in Java object oriented programming

This encapsulation of variables 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!

>>>Return to Java Basics Tutorial Page


Related topics

  1. Object Creation Using new Operator in Java
  2. Object class in Java
  3. this Keyword in Java With Examples
  4. finalize() Method in Java
  5. Java OOP Interview Questions And Answers

You may also like-

  1. Java Abstract Class and Abstract Method
  2. Java Pass by Value or Pass by Reference
  3. Automatic numeric type promotion in Java
  4. final Keyword in Java With Examples
  5. super Keyword in Java With Examples
  6. Deadlock in Java multi-threading
  7. ThreadLocal class in Java
  8. Method Reference in Java