Methods

public class SavingsAccount {
  
  int balance;
  
  public SavingsAccount(int initialBalance){
    balance = initialBalance;
  }

  public void checkBalance(){
    System.out.println("Hello!");
    System.out.println("Your balance is " + balance);
  }

  public void deposit(int amountToDeposit){
    int newBalance = balance + amountToDeposit;
    balance = newBalance;
    System.out.println("You just deposited " + amountToDeposit);
  }

  public int withdraw(int amountToWithdraw){
    int afterWithdraw = balance - amountToWithdraw;
    balance = afterWithdraw;
    return amountToWithdraw;
  }


  
  public static void main(String[] args){
    SavingsAccount savings = new SavingsAccount(2000);
    
    //Check balance:
    // System.out.println("Hello!");
    // System.out.println("Your balance is "+savings.balance);
    savings.checkBalance();
    
    //Withdrawing:
    // int afterWithdraw = savings.balance - 300;
    // savings.balance = afterWithdraw;
    // System.out.println("You just withdrew "+300);
    savings.withdraw(300);
    
    //Check balance:
    // System.out.println("Hello!");
    // System.out.println("Your balance is "+savings.balance);
    savings.checkBalance();

    //Deposit:
    // int afterDeposit = savings.balance + 600;
    // savings.balance = afterDeposit;
    // System.out.println("You just deposited "+600);
    savings.deposit(600);
    
  }       
}

In the terminal it is showing

Hello!
Your balance is 2000
Hello!
Your balance is 1700
You just deposited 600

first two lines I understand why it is printed but I am not able to understand why the rest of the lines are printed.
please someone help it is very confusing.

every time you run savings.checkBalance(); or savings.deposit(); or savings.withdraw(300); the actions from those functions are called.

Some of those actions include print statements, so the clue to your question is in those functions.

but in withdraw() method there is no print statement then how is it printing

Hello!
Your balance is 1700

hello i’m new at this too. i spent hours on that one. if i understand your question correctly it can be answered by:

svings.withdraw(300);

you are calling the withdraw method(or behaviour) and the amountToWithdraw is set as 300. could somebody better at this than i confirm this or set me straight?

oops i misunderstood your question?

Ok ok my bad I didn’t see
I thought that I am calling only 3 methods but I am calling 4 methods
I didn’t see that