July 21, 2022

Object in Java

Once you create a class in Java that defines a new data type which can be used for creating object of that data type (class). Thus object in Java is an instance of the class, which gets its state and behavior from the class.

Fields defined within a class are also known as instance variables because each instance of the class (object) gets its own copy of these variables. Thus the fields provide state to each object.

Methods with in the class define the behavior of its objects. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.

Creating an object in Java

Creation of an object in Java is comprised of three parts-

  1. Declaration- In this step you declare a variable of the class type. Suppose you have a class called MyClass then you can declare an object of type MyClass as-
    MyClass myObj;
    	
  2. Instantiation- You create an object using the new operator. The new operator returns a reference to the object it created which is assigned to the declared variable (Step 1). For example- myObj = new MyClass();
  3. Initialization- Creation of an object using the new operator is followed by a call to a constructor, which initializes the new object.

Generally in your code you will combine the above mentioned three steps into one statement for creating object of a class in Java.

MyClass myObj = new MyClass();

Java object creation example

Let us see the whole process of creating an object in Java with an example. The class used is as follows.

class MyClass {
  int num;
  String name;
  // Constructor
  MyClass(int num, String name){
    this.num = num;
    this.name = name;
  }
  public void displayValues() {
    System.out.println("Num- " + num + " Name- " + name);
  }
  ..
  ..
}

You will declare a variable of type MyClass as following-

MyClass myObj;

This notifies the compiler that you will use myObj to refer to data whose type is MyClass. Declaring a reference variable does not create an object at this stage it is just a reference variable which currently references no object. Following figure illustrates this stage of the object creation.

object in Java declaration

The following statement instantiates a class by allocating memory for a new object and returning a reference to that memory.

myObj = new MyClass(10, "knpCode"); 

The new operator also invokes the object constructor to initialize the created object. The result of executing this statement can be illustrated in the next figure:

object in Java instantiation

Bundling code into individual software objects provides a number of benefits, including:

  • Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
  • Information-hiding: By interacting only with an object's methods not directly with its fields, the details of its internal implementation remain hidden from the outside world.
  • Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to create properly coded and tested task-specific functionalities and package them as jars. Which you can then use in your own code by creating objects of those classes.
  • Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

Referencehttps://docs.oracle.com/javase/tutorial/java/concepts/object.html

That's all for the topic Object in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment