Java Methods: Bank Account Balance Example

💻 Understanding Java Methods Using an Account Balance Example In Java, a method (function) is a block of code that performs a specific task. It helps keep programs clean and organized. Let’s understand with a simple bank account example 🏦 public class BankAccount { static double accountBalance = 1000; // Method to deposit money public static void deposit(double amount) { accountBalance = accountBalance + amount; } // Method to withdraw money public static void withdraw(double amount) { accountBalance = accountBalance - amount; } // Method to show balance public static void checkBalance() { System.out.println("Balance: " + accountBalance); } public static void main(String[] args) { deposit(500); // Add money withdraw(200); // Take money checkBalance(); // Show final balance } } 🔹 What are we learning? ✔ A method is used to do one task ✔ deposit() adds money ✔ withdraw() removes money ✔ checkBalance() shows current balance Instead of writing the same code again and again, we just call the method when needed. 📌 Simple idea: Methods make your code reusable and easy to understand. #Java #CodingBasics #ProgrammingForBeginners

To view or add a comment, sign in

Explore content categories