Popular Posts

July 11, 2024

What is object and how to declare the Object in Java

 

In Java, an object is an instance of a class. It is a concrete realization of a class, containing actual values instead of variables. Objects in Java are created using the new keyword followed by the class constructor.

Declaring and Creating an Object in Java

To declare and create an object in Java, you follow these steps:

  1. Declaration: Define a variable of the class type.
  2. Instantiation: Use the new keyword to create the object.
  3. Initialization: Call the class constructor to initialize the object.

Here's the syntax:

ClassName objectName = new ClassName(parameters);

Example

Let's use the Car class from the previous example to create an object:

Step 1: Define the Car Class

public class Car {
    // Fields (variables)
    private String color;
    private String model;

    // Constructor
    public Car(String color, String model) {
        this.color = color;
        this.model = model;
    }

    // Methods
    public void displayInfo() {
        System.out.println("Car model: " + model + ", Color: " + color);
    }

    // Getter and Setter methods
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

Step 2: Create and Initialize an Object


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

        // Calling methods on the object
        myCar.displayInfo(); // Output: Car model: Toyota, Color: Red

        // Using getter and setter methods
        myCar.setColor("Blue");
        System.out.println("Updated Color: " + myCar.getColor()); // Output: Updated Color: Blue
    }
}

Breakdown

  1. Declaration: Car myCar;

    • This declares a variable myCar of type Car.
  2. Instantiation: new Car("Red", "Toyota");

    • The new keyword creates a new instance of the Car class.
  3. Initialization: Car("Red", "Toyota");

    • The Car constructor is called to initialize the new object with the specified values.

Object Methods

Once an object is created, you can use its methods to interact with the object's data. Methods can include:

  • Getters and Setters: To access and modify private fields.
  • Other Methods: To perform operations related to the object's behavior.

Example with Multiple Objects

You can create multiple objects from the same class:

public class Main {

    public static void main(String[] args) {

        // Creating multiple objects of the Car class

        Car car1 = new Car("Red", "Toyota");

        Car car2 = new Car("Blue", "Honda");


        // Calling methods on the objects

        car1.displayInfo(); // Output: Car model: Toyota, Color: Red

        car2.displayInfo(); // Output: Car model: Honda, Color: Blue

    }

}

Summary

  • Object: An instance of a class containing actual data.
  • Declaration: Define a variable of the class type.
  • Instantiation: Use the new keyword to create an object.
  • Initialization: Call the class constructor to set initial values.

Objects are the core concept of object-oriented programming in Java, allowing developers to model real-world entities and their interactions within a program.


No comments:
Write comments