Methods in Java: Static and Instance Methods Explained with Examples
Methods in Java A method in Java is a block of statements that is used to perform a specific task and may return a result. Methods help improve code reusability, readability, and modular programming. Java methods are mainly classified into two types: Static Method Instance Method 1. Static Method ✅ Definition A static method is a method that is declared using the static keyword inside a class. It belongs to the class, not to any specific object. Static methods are generally used to perform common operations that are the same for all users. 🔹 Syntax static returnType methodName() { // method body } 🌍 Real-Life Example ATM Bank Interest Rate Interest calculation formula is the same for all customers So it can be implemented as a static method 🧠 Example Code class Calculator { static int add(int a, int b) { return a + b; } } public class Main { public static void main(String[] args) { ...