Objects in Java

  • Last updated Apr 25, 2024

In Java, objects are instances of classes that have identity, states, and behaviors.

  • Identity: The name by which an object is identified.
  • States: States are properties of an object. For example, a dog has attributes like name, color, and weight.
  • Behaviors: Behaviors are methods (functions). For example, a dog has behaviors such as barking, eating, and running.

Think of objects as real-world entities that possess attributes (data) and behaviors (methods). For instance, in a banking application, you might have an Account class representing individual bank accounts. Each account object would encapsulate specific information such as the account holder's name, balance, and methods like deposit and withdraw.

Creating Objects

To bring an object to life in Java, you first need to define a class. A class acts as a blueprint for objects, outlining their structure and behavior. Once a class is defined, you can create multiple instances (objects) based on that blueprint. The new keyword is used for object instantiation, as shown in the following example:

ClassName objectName = new ClassName();

Here's a breakdown and an explanation of the syntax:

  • ClassName: This is the name of the class. Replace it with the name of the class for which you want to create an object.
  • objectName: This represents the name of your object. Choose a suitable name for your object; it will be the variable that references your new object.
  • new: This keyword is used to allocate memory and create an instance of the class.
  • ClassName(): This part involves calling the constructor of the class. If the class has a parameterized constructor, you would provide the necessary arguments within the parentheses.

Here's a simple example:

// Define a class
class Car {
    // Class members and methods would go here
}

// Create a new object of the Car class
Car myCar = new Car();
Object Initialization

After creating an object, the next step is initializing its attributes. This ensures that the object starts with valid and meaningful data. Java provides constructors for this purpose. Constructors are special methods within a class that are called when an object is created. They initialize the object's state. Here's an example:

public class Car {
    String make;
    String model;
    int year;

    // Constructor
    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    // Additional method (for demonstration purposes)
    public void displayCarInfo() {
        System.out.println("Make: " + make);
        System.out.println("Model: " + model);
        System.out.println("Year: " + year);
    }

    public static void main(String[] args) {
        // Creating an object of the Car class
        Car myCar = new Car("Toyota", "Camry", 2022);
        
    }
}

In this example, a Car object named myCar is created using the new keyword. Following that, we provide the required parameters (make, model, and year) to the constructor, specifying Toyota as the make, Camry as the model, and 2022 as the year.

Accessing Object Members

After creating an object, you can access its members (fields and methods) using dot notation. For example:

public class Car {
    String make;
    String model;
    int year;

    // Constructor
    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    // Method
    public void displayCarInfo() {
        System.out.println("Make: " + make);
        System.out.println("Model: " + model);
        System.out.println("Year: " + year);
    }

    public static void main(String[] args) {
        // Creating an object of the Car class
        Car myCar = new Car("Toyota", "Camry", 2022);

        // Accessing data members
        String carMake = myCar.make;
        String carModel = myCar.model;
        int carYear = myCar.year;

        // Invoking methods
        myCar.displayCarInfo();
    }
}
Objects Naming Convention

There are different naming convention in every programming languages. In Java, an object name must always be in the camelCase form.

Camel case is a naming convention in which the first letter of each word in a compound word is in uppercase except the letter of first word. Example: myObject, dateTime, createDate, totalAmount.