Java Tutorial # 7 : Methods

Methods

are blocks of code that are only executed when called.

Methods are used to carry out functions to the data that are passed through the methods parameters.

Methods are useful because they allow code to be reused easily.

class Main {
  static void exMethod() {
    System.out.println("Example method");
  }
  public static void main(String[] args) {

//calling method  
  exMethod();
  }
}
Output
Example method

Two Parameter Method

If you want a method to return a value. Use the keyword return 

class Main {
  static int exMethod(int a, int b) {
    return a / b;
  }

  public static void main(String[] args) {
    int c = exMethod(10, 5);
    System.out.println(c);
  }
}
Output
2

Method with If statement

class Main {
    
// Creating method with a variable called payment
static void accountBalance(int payment) {

   // if Statement conditions
    if (payment >= 75) {
      System.out.println("Customer paid in full");
    } else {
      System.out.println("Hold account! Has Unpaid charges");
    }

  }
  public static void main(String[] args) {
    
   // Calling method
   accountBalance(65);
  }
}
Output
Hold account! Has Unpaid charges