Java Methods: Code Reusability and Organization

🚀 Day 11 at Tap Academy – Methods in Java ☕💻 ☘️Today I learned one of the most powerful concepts in Java – Methods. Methods help us organize, reuse, and simplify our code. Let’s understand it in a simple way 👇 🔹 What is a Method? A method is a block of code that performs a particular task. It is also called a function in Java. ✅ Methods help in: ♻️ Code Reusability (use same code many times) 📉 Code Reduction (avoid writing duplicate code) 🧩 Code Organization and Documentation Instead of writing the same code again and again, we can just call the method multiple times. 🔹 Methods and Objects Relationship Objects have: 🧾 State (Has) → Data (Data Types) ⚙️ Behavior (Does) → Methods Ex: A Car 🚗 Has → color, speed, model (State) Does → start(), stop(), accelerate() (Behavior → Methods) 🔹 Syntax of a Method access_modifier return_type methodName(input) { // method body } Ex: public void display() { System.out.println("Hello Java"); } ✔ access modifier → public, private,etc. ✔ return type → void, int, String,etc. ✔ method name → display() ✔ parentheses () → input parameters If method returns nothing → use void 🔹 4 Types of Methods in Java 1️⃣ No Input, No Output public void greet() { System.out.println("Hello Student"); } Call: obj.greet(); 📌Working: Heap → object stored Stack → greet() method called → prints output → removed from stack 2️⃣ Input, No Output public void printNumber(int num) { System.out.println(num); } Call: obj.printNumber(10); 📌 Working: Heap → object created Stack → printNumber(10) stored → executes → removed 3️⃣ No Input, Output public int getNumber() { return 100; } Call: int result = obj.getNumber(); 📌 Working: Stack → getNumber() runs returns 100 → stored in variable 4️⃣ Input, Output public int add(int a, int b) { return a + b; } Call: int sum = obj.add(10, 20); 📌 Working: Stack → add(10,20) executes returns 30 → stored in sum Execution → Result → Stack cleared 🔹 Types of Methods in Java 1️⃣ User-Defined Methods Methods created by programmer Ex: public void display() { System.out.println("User defined method"); } 2️⃣ Inbuilt Methods Methods already provided by Java Ex: Scanner sc = new Scanner(System.in); sc.nextInt(); sc.nextFloat(); sc.nextDouble(); sc.nextLine(); 🔹 Beauty of Methods ✨ Instead of writing: System.out.println("Hello"); System.out.println("Hello"); Write once: public void sayHello() { System.out.println("Hello"); } Call many times: obj.sayHello(); obj.sayHello(); ✔ Cleaner Code ✔ Less Code ✔ Better Structure 🔹 Key Points Summary ✔ Method = block of code ✔ Used for reuse and reduce code 🎯 My Learning Outcome – Day 11 Today I understood how methods make Java programs more structured, reusable, and efficient. Methods are the backbone of object behavior in Java. Learning step by step towards becoming a better Java developer 🚀 #Java #Methods #CoreJava #Programming #JavaLearning #TapAcademy #CodingJourney #JavaDeveloper #LearnJava #CodeReuse #ObjectOrientedProgramming

  • graphical user interface

To view or add a comment, sign in

Explore content categories