Object
An object is a real, usable instance created from a class. While a class is just a design, an object is the actual "thing" built using that design, with its own actual values stored in memory.
1. What is an Object?
An object is a real, usable instance created from a class. While a class is just a design, an object is the actual "thing" built using that design, with its own actual values stored in memory.
2. Why is it used?
Objects let a program actually work with real data based on a class's structure — like creating an actual Student object with a real name and roll number, rather than just describing what a Student should look like.
3. Real-Life Example
If a class is a blueprint for a house, an object is an actual house built from that blueprint, standing on real land, with a real address, and people actually living in it.
4. Syntax
javaClassName objectName = new ClassName();
5. Example Program
javaclass Car { String model; } public class ObjectDemo { public static void main(String[] args) { Car car1 = new Car(); car1.model = "Sedan"; System.out.println("Car model: " + car1.model); } }
Output:
Car model: Sedan6. Key Points to Remember
- The
newkeyword is used to create an object in Java. - Multiple objects can be created from the same class, each holding its own separate data.
- Objects are stored in a memory area called the Heap.