In Java, everything starts with objects. Whether you're building a banking system, a video game, or an e-commerce platform, objects are the building blocks. If classes are blueprints, objects are the actual houses built from those blueprints.
What is an Object in Java?
An object is a runtime instance of a class. It is an entity that has:
- State (fields/attributes): Describes the object (e.g., name, age, balance)
- Behavior (methods): What the object can do (e.g., walk, talk, deposit)
How to Create an Object in Java
To create an object, we use the new keyword followed by the class constructor. Here's a simple example:
class Dog {
String name;
int age;
void bark() {
System.out.println(name + " says: Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog(); // Creating an object
myDog.name = "Buddy";
myDog.age = 3;
myDog.bark(); // Calling a method on the object
}
}
Buddy says: Woof!
Explanation
In the above program:
Dogis the class – the blueprint.myDogis the object – the actual dog instance.- We set the dog's name and age using dot notation, and then made it bark using
myDog.bark().
Multiple Objects from One Class
You can create as many objects as you want from a single class. Each will have its own copy of fields and methods.
public class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.name = "Max";
dog1.age = 4;
Dog dog2 = new Dog();
dog2.name = "Bella";
dog2.age = 2;
dog1.bark();
dog2.bark();
}
}
Max says: Woof!
Bella says: Woof!
Understanding Memory Allocation
When you create an object using new, Java allocates memory on the heap. The variable (myDog) stores a reference (address) to that memory location.
Constructor: The Object Initializer
Instead of setting each field manually, we can initialize values using a constructor.
class Dog {
String name;
int age;
Dog(String n, int a) {
name = n;
age = a;
}
void bark() {
System.out.println(name + " barks at age " + age);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Rocky", 5);
dog.bark();
}
}
Rocky barks at age 5
Object Methods vs Class Methods
Methods that work on object-specific data (like bark()) are instance methods. You call them through objects. We'll cover static (class-level) methods in another tutorial.
Real-world Analogy
Think of a class like a "Car" manual, and an object like an actual car you can drive. Multiple people can build cars from the same manual, but each car has its own color, speed, and mileage.
Why Objects Matter
Java is an Object-Oriented Programming language. Understanding objects is foundational for:
- Encapsulation (wrapping data and behavior)
- Inheritance (creating hierarchical classes)
- Polymorphism (methods behaving differently)
- Abstraction (hiding internal complexities)
Summary
To recap:
- Objects are created from classes.
- They hold data in fields and define behavior through methods.
- The
newkeyword is used to create object instances. - Constructors help initialize objects conveniently.
