What is meant by object in Java?


Introduction

In real life, we can call tables, chairs, lamps, etc. objects as they have some attributes and functions. We can say that anything that has attributes or properties and some functionality like a lamp has a stand and a bulb and it can light up a room so it is an object. In Java also, we have objects and they have their own properties. Objects are basically the instance of the class and the class provides a blueprint for the creation of an object. Let’s have a brief discussion on objects and how to create an object in Java.

Definition

To master object-oriented technology, you must be familiar with objects. Object-oriented programming aims to create programming that functions similarly to the actual world. It also emphasizes how data are connected. There are several OOPs principles, Object being one of them. The object is a distinct entity with its own distinct traits and behavior. A class is the object's blueprint. Therefore, an instance of the class is an object. In this part, we will discuss the Java object definition syntax. The characteristics that the objects share are as follows −

  • Identity − The identity is a feature used to uniquely identify that item, such as a random ID number or a memory location. Simpler objects, such as a lamp, may only have two states (on and off) and two actions (turn on, turn off), but they nevertheless have an identity (for example, the item's manufacture ID). Any outside user would be unable to see the ID's actual value. It's what the JVM uses to track objects internally.

  • State − Variables store the state of an object (fields).

  • Behavior − The methods illustrate an object's behavior.

How to create an object in Java?

There are following steps to create the object −

  • Declaration

  • Instantiating object

  • Initializing an object

Syntax to create an object

Declaration

The declaration primarily defines the object's data type. Declarations advise the compiler that you want to use the given name to refer to a variable of the specified type. Objects that are declared are not instantiated. Create a Date object or any other object with the new operator.

Class_name Object_name;

The object’s data type is always the name of the class.

Instantiating

Memory is allocated for a newly generated object via the new operator. new requires a single parameter consisting of the object's constructor method. The constructor is responsible for initializing the new object. In other words, instantiation is the process of creating an instance/object of a class. It occupies the initial memory of the object and returns a reference. An instantiation of an object acts as the blueprint for a class in Java. There are two ways for instantiation −

  • Using new keyword − For instantiating a class, Java provides a new keyword. Let’s see how to instantiate a class using a new keyword.

//declaring an object
Dog Mydog;
//Instantiating using new keyword
Mydog=new Dog();

Here Dog() is the constructor of the class and is a non-parameterized constructor, i.e there are no arguments. But if the constructor was parameterized then we have to initialize the arguments also,for eg,

Mydog=new Dog(2,Bruno);

Let’s see an example for better understanding.

Example

public class Dog{
   int age;
   String name;
   Dog(int a, String n)//parameterized constructor
   {
      age=a;
      name=n;
   }
   public void display(){
      System.out.println(age);
      System.out.println(name);
   } 
   public static void main(String args[]){
      Dog mydog=new Dog(12, "Bruno");//Object instantiation and object declaration
      mydog.display();//object initialization
   }
}

Output

12
Bruno
  • Using static factory method − A class can also be instantiated by executing a static factory function. A class may offer a public static factory method, which is only a static method that returns an instance of the class. Always keep in mind that it differs from the factory method pattern.

Let’s see an example of a static factory method

Example

public class Dog{
   int age;
   String name;
   Dog(int age, String name)//parameterized constructor
   {
      this.age=age;
      this.name=name;
   }
   public static Dog mydog(int age, String name)
   {
      return new Dog(age,name);
   } 
   public static void main(String args[])
   {
      Dog d=new Dog(23,"bruno");
      System.out.println(d.age+" "+d.name);
   }
}

Output

23 bruno

Initializing

Classes include constructor methods to initialize a newly created instance of that type. Constructors may be identified from other methods in a class declaration since they have the same name as the class and have no return type.

There are mainly four ways to initialize an object they are −

  • Naïve method − The concept is to obtain an instance of the class using the new operator and to set its values using the class setters.

  • Constructor − When using the new operator to create an object, a constructor must be specified. A constructor has the same name as the class and returns nothing. It can accept a collection of parameters, which are the fields we wish to set values for, or it can be parameter-free (no-arg constructor). If we declare a class with no constructors, the compiler will automatically generate one.

  • Copy Constructor − A copy constructor is a special constructor that creates a new object from an existing object. It accepts only a single parameter, which is another instance of the same class. Using this() expression, we may directly execute another constructor from the copy constructor.

  • Anonymous inner class − Use "Double Brace Initialization" as an alternate method for initializing an object. This generates a single instance initializer within an unnamed inner class. This technique is strongly discouraged.

Conclusion

Here in this article, we have learned about what is meant by an object and how an object is created. An object is the basic building block of an object-oriented programming language. Before starting with Java you must be familiar with what an object is. Class is the blueprint of an object so we can say that an object is an instance of a class.

Updated on: 23-Aug-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements