🧑💻 Classes and Methods in Java – Explained with Examples
When you write programs in Java, everything revolves around classes and methods. They are the building blocks of Object-Oriented Programming (OOPs).
📦 What is a Class?
A class is like a blueprint or template for creating objects.
-
It defines variables (data members) → the state of the object.
-
It defines methods (functions) → the behavior of the object.
👉 Think of a class as a recipe. The recipe describes how to make a cake, but you can bake many cakes (objects) using it.
🛠️ What is a Method?
A method is a block of code inside a class that performs a specific task.
-
Helps in code reusability
-
Makes programs organized and readable
👉 Think of a method as a chef’s action in the recipe → mixing ingredients, baking, decorating.
✨ Syntax
Class in Java
class ClassName {
// variables
// methods
}
Method in Java
returnType methodName(parameters) {
// code to execute
return value; // optional
}
📖 Example: Class & Method in Action
class Car {
// Data members (variables)
String brand;
int speed;
// Method
void drive() {
System.out.println(brand + " is driving at " + speed + " km/h");
}
}
public class Main {
public static void main(String[] args) {
// Creating objects
Car car1 = new Car();
car1.brand = "Tesla";
car1.speed = 120;
car1.drive();
Car car2 = new Car();
car2.brand = "BMW";
car2.speed = 150;
car2.drive();
}
}
✅ Output
Tesla is driving at 120 km/h
BMW is driving at 150 km/h
🔑 Key Takeaways
-
Class = Blueprint (defines variables + methods)
-
Object = Real-world instance created from a class
-
Method = Behavior/action of the class
-
Classes & methods make code modular, reusable, and closer to real-world modeling
🎯 Real-World Analogy
-
Class = Recipe 🍰
-
Object = Cakes baked using recipe 🎂🎂🎂
-
Method = Actions like mixing, baking, decorating 👩🍳
✅ With classes and methods, Java becomes powerful in simulating the real world inside code.
📘 Examples of Classes and Methods in Java
1️⃣ Student Example 🎓
class Student {
// Variables
String name;
int age;
// Method
void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Amit";
s1.age = 20;
s1.displayInfo();
Student s2 = new Student();
s2.name = "Priya";
s2.age = 22;
s2.displayInfo();
}
}
✅ Output:
Name: Amit, Age: 20
Name: Priya, Age: 22
👉 Shows how class → Student and method → displayInfo() work together.
2️⃣ Calculator Example 🔢
class Calculator {
// Method with parameters
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Sum: " + calc.add(5, 3));
System.out.println("Product: " + calc.multiply(4, 6));
}
}
✅ Output:
Sum: 8
Product: 24
👉 Demonstrates methods with return values and parameters.
3️⃣ Bank Account Example 🏦
class BankAccount {
String accountHolder;
double balance;
// Method to deposit money
void deposit(double amount) {
balance += amount;
System.out.println(amount + " deposited. New balance: " + balance);
}
// Method to withdraw money
void withdraw(double amount) {
if(amount <= balance) {
balance -= amount;
System.out.println(amount + " withdrawn. New balance: " + balance);
} else {
System.out.println("Insufficient balance!");
}
}
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.accountHolder = "Ravi";
acc.balance = 5000;
acc.deposit(2000);
acc.withdraw(1500);
acc.withdraw(7000);
}
}
✅ Output:
2000.0 deposited. New balance: 7000.0
1500.0 withdrawn. New balance: 5500.0
Insufficient balance!
👉 A real-world simulation where methods define actions (deposit/withdraw).
4️⃣ Simple Greeting Example 👋
class Greeter {
void sayHello(String name) {
System.out.println("Hello, " + name + "! Welcome to Java.");
}
}
public class Main {
public static void main(String[] args) {
Greeter g = new Greeter();
g.sayHello("Sunita");
g.sayHello("Rahul");
}
}
✅ Output:
Hello, Sunita! Welcome to Java.
Hello, Rahul! Welcome to Java.
👉 Engages beginners with a friendly, relatable example.