Object in Java
An object is an instance of a class.
When you create an object, you are creating a specific example of a class with its own unique set of values for the attributes defined by the class.
Think of a class as a blueprint (like the blueprint of a house), and an object as the actual house built from that blueprint.
Key Characteristics of Objects
State: The data or attributes of the object.
Behavior: The methods or functions that the object can perform.
Identity: A unique reference to the object, typically defined by its memory address.
Creating an Object
To create an object in Java, you use the new keyword followed by a call to a constructor method of the class.
Example: Creating and Using an Object
Let's look at an example to understand how to create and use an object in Java.
Defining the Class
class Car {
// Fields (Attributes)
String color;
String model;
int year;
// Constructor
public Car(String color, String model, int year) {
this.color = color;
this.model = model;
this.year = year;
}
// Method
public void displayDetails() {
System.out.println("Car Model: " + model);
System.out.println("Car Color: " + color);
System.out.println("Car Year: " + year);
}
}
Creating and Using an Object
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car("Red", "Toyota", 2020);
// Accessing object's attributes
System.out.println("My Car's Model: " + myCar.model);
// Calling object's method
myCar.displayDetails();
}
}
Explanation:
Creating an Object: Car myCar = new Car("Red", "Toyota", 2020); creates an instance of the Car class and initializes it with the given values.
Accessing Attributes: System.out.println("My Car's Model: " + myCar.model); accesses and prints the model attribute of the myCar object.
Calling Methods: myCar.displayDetails(); calls the displayDetails method of the myCar object to print its details.
The output will be:
My Car's Model: Toyota
Car Model: Toyota
Car Color: Red
Car Year: 2020
Objects and Memory
When an object is created, it is allocated memory in the heap.
The reference to this memory location is stored in a variable.
This reference allows you to interact with the object.
Example:
Car myCar = new Car("Red", "Toyota", 2020);
In this line, myCar is a reference variable that holds the memory address of the Car object created in the heap.
Methods and Objects
Objects can have methods that define their behaviors.
You can call these methods to perform actions on the object's data.
Example:
myCar.displayDetails();
This line calls the displayDetails method of the myCar object, which prints the details of the car.
Multiple Objects
You can create multiple objects from the same class, each with its own set of values for the attributes.
Example:
Car car1 = new Car("Red", "Toyota", 2020);
Car car2 = new Car("Blue", "Honda", 2018);
car1.displayDetails();
car2.displayDetails();
The output will be:
Car Model: Toyota
Car Color: Red
Car Year: 2020
Car Model: Honda
Car Color: Blue
Car Year: 2018
In this example, car1 and car2 are two different objects created from the Car class, each with its own attribute values.
Summary
An object is an instance of a class. It has a state (attributes), behavior (methods), and identity (unique reference).
Creating an Object: Use the new keyword followed by a constructor to create an object.
Accessing Attributes: Use the dot (.) operator to access an object's attributes.
Calling Methods: Use the dot (.) operator to call an object's methods.
Multiple Objects: You can create multiple objects from the same class, each with its own set of values for the attributes.
By understanding objects, you'll be able to create and manipulate instances of classes, which is a core aspect of Object-Oriented Programming in Java.