Class
A class is a blueprint that defines the properties (variables) and behaviours (methods) that its objects will have. It doesn't hold real data by itself — it just describes the structure that objects created from it will follow.
1. What is a Class?
A class is a blueprint that defines the properties (variables) and behaviours (methods) that its objects will have. It doesn't hold real data by itself — it just describes the structure that objects created from it will follow.
2. Why is it used?
Classes let you model real-world things in code in an organized way. Instead of scattering related data and actions randomly, a class groups everything about one type of "thing" (like a Student or a Car) together in one place.
3. Real-Life Example
Think of a blueprint for a house. The blueprint itself is not a house you can live in — it just describes how each house built from it should look, with rooms, doors, and windows in specific places. A class is exactly this kind of blueprint for objects.
4. Syntax
javaclass ClassName { // fields (variables) // methods }
5. Example Program
javaclass Student { String name; int rollNumber; } public class ClassDemo { public static void main(String[] args) { Student s = new Student(); s.name = "Aditi"; s.rollNumber = 12; System.out.println(s.name + " - " + s.rollNumber); } }
Output:
Aditi - 126. Key Points to Remember
- A class by itself takes no memory for data — memory is used only when an object is created from it.
- A class can contain fields, methods, constructors, and more.
- One Java file can have multiple classes, but only one can be
public.