Classes and objects are the two main aspects of object-oriented programming. A class is a blueprint for creating objects, and an object is an instance of a class.
💡 Analogy time:
- Class: Think of a class as a cookie cutter. It defines the exact shape and size of the cookie. However, the cookie cutter itself is not a cookie. You cannot eat it.
- Object: The actual cookies created using that cookie cutter. You can stamp out as many cookies (objects) as you want from that one cutter (class). Once the objects are created, you can decorate each one differently!
In the real world, almost every object has two characteristics:
When you define a class, you are deciding exactly what state and behavior all objects of that type will possess.
To create a class, use the class keyword. By convention, class names in Java start with a capital letter.
In this example, we create a simple class named Car with a single attribute color.
public class Car {
String color = "red";
}
Note: The
Carclass itself won't produce any output on its own. In the live editor, it is wrapped in a smallMainclass so you can run it.
To create an object of a class, you specify the class name, followed by the object name, and use the new keyword.
The new keyword is a Java operator that creates the object.
🧠 Deep Concept: What does
newactually do? When you use thenewkeyword, you are telling the Java Virtual Machine (JVM): "Hey, look at the blueprint for this Class, and build me a real one in memory!" The JVM goes to a special area of memory called the Heap, carves out enough space to hold all the object's attributes, and returns the "address" (reference) of that object back to your variable.
Let's create an object called myCar from our Car class.
public class Main {
// The Car class definition
public static class Car {
String color = "red";
}
public static void main(String[] args) {
// Create an object of the Car class
Car myCar = new Car();
// Access the object's attributes and print them
System.out.println("The car's color is: " + myCar.color);
}
}
You can create multiple objects from one class.
public class Main {
static class Car {
String color = "red";
}
public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car();
car2.color = "blue"; // Change the color of car2
System.out.println(car1.color); // Outputs "red"
System.out.println(car2.color); // Outputs "blue"
}
}
Notice how changing the color of car2 did not affect car1. Even though they were created from the exact same blueprint, they are two completely distinct objects sitting in two different locations in memory. They have independent state.
If we keep using the new keyword to create objects on the Heap memory, won't our computer eventually run out of RAM and crash?
In older languages like C or C++, developers had to manually write code to "destroy" objects and free up memory. If they forgot, it caused a Memory Leak, eventually crashing the system.
Java handles this automatically! Java employs a built-in superhero called the Garbage Collector (GC).
🧹 The "Hotel Housekeeping" Analogy: The Garbage Collector is like an invisible hotel housekeeper that constantly roams the halls of your computer's memory. It looks for objects (rooms) that no longer have any variables (keys) pointing to them. If it finds an abandoned object that your code can no longer reach, it automatically sweeps it up and throws it in the trash, freeing up the memory space for new objects!
== compares object references, while equals() should compare meaningful object content when a class defines value equality.equals(), hashCode(), and toString() together.What keyword is used to create an object of a class?