What is an object?

In general, entities with state and behavior are known as objects. The Java object is an instance of a Class that has some state, behavior and unique identification.

In object-oriented programming, an object represents the real-life entity such as Person, Car, Fruit, etc. Or in simple words, we can say anything that has some state, behavior and unique representation can be represented as a Java object.

How to create an object?

We create an object by initiating a class. All instances share class attributes and behavior however, the values of these attributes, or states, are unique for each object. A single class can have N number of instances.

Syntax

To create an object of a class we use keyword ‘new’.

<Class> <object_name> = new <Class>();

Example

Following is the example that shows how you can create an object/instance of a class.

Employee john = new Employee();

In the above example, Employee is the class that may contain information about the employee Like –

  1. First name
  2. Last name
  3. Age
  4. Date of Join
  5. Salary
  6. etc…

The object name ‘john’ is the instance of that particular class. You can compare this object with variables name we create to hold different data of different types. Similarly, object name ‘john’ is going to hold information about an employee.

Fruit example

In the following example, we have one class called ‘Fruit’ which is used to store information about different types of fruit and the driver class FruitExample containing main() method where we create multiple objects of the class Fruit.

public class Fruit {

    // Fruit name
    String name;

    // Fruit color
    String color;

    // Size
    double size;

    // Price
    double price;

    public void print() {
        System.out.println("Fruit name: " + name);
        System.out.println("Color: " + color);
        System.out.println("Size: " + size);
        System.out.println("Price: " + price);
        System.out.println("------------------");
    }
}
public class FruitExample {

    public static void main(String[] args) {

        // Apple
        Fruit apple = new Fruit();

        apple.name = "Apple";
        apple.color = "RED";
        apple.size = 8.0; // In cm
        apple.price = 150; // KG

        apple.print();

        // Orange
        Fruit orange = new Fruit();

        orange.name = "Orange";
        orange.color = "ORANGE";
        orange.size = 7.5; // In cm
        orange.price = 100; // KG

        orange.print();

        // Banana
        Fruit banana = new Fruit();

        banana.name = "Banana";
        banana.color = "YELLOW";
        banana.size = 9; // In inch
        banana.price = 50; // KG

        banana.print();

    }
}

Output

As in the previous tutorial, here both classes need to be in the same directory. And then compile and run the class FruitExample.

>> javac FruitExample.java
>> java FruitExample
Fruit name: Apple
Color: RED
Size: 8.0
Price: 150.0
------------------
Fruit name: Orange
Color: ORANGE
Size: 7.5
Price: 100.0
------------------
Fruit name: Banana
Color: YELLOW
Size: 9.0
Price: 50.0
------------------