DEV Community

Cover image for JAVA METHODS
Mary-softeng
Mary-softeng

Posted on

JAVA METHODS

Java methods are block of codes which runs only when they are called and they are also known as functions since they can perform certain actions.
How To Create Methods
A method must be declared within a class and it is defined with the word method, followed by parentheses ().

public class Main{
  static void myMethod(){
  }
}
Enter fullscreen mode Exit fullscreen mode

myMethod() is the name of the method
static means that the method belongs to the Main class and not an object of the main class.
void means that this method does not have a return value.

Call A Method
you call a method by writting the name of the method followed by two parentheses and a semicolon;
A method can also be called multiple time.

pubblic class Main{
  static void myMethod(){
    System.out.println("I am Java Method");
   }
 public static void main(String[] args) {
   myMethod();
   }
}
Output
I am Java Method
Enter fullscreen mode Exit fullscreen mode

Java Method Parameters
Parameters and Arguments
Parameters act as variables inside the method. They are specified after the method name , inside parentheses and they can be as many as possible separated with a comma.

public class Main {
  static void myMethod(String fname, int age){
    System.out.println(fname + "is" +age);
  }
  public static void main(String[] args) {
   myMethod("Mary", 23);

  }
}
output
Mary is 23
Enter fullscreen mode Exit fullscreen mode

Return Value
For a method to return a value, use primitive data types such as(int,char,etec) instead of void, and use the return keyword inside the method:

public class Main{
  static int myMethod(int x) {
    return 7 + X;
   }
 public static void main(String[] args) {
   System.out.println(myMethod(5));
  }
}
outputs
12(7+5)
Enter fullscreen mode Exit fullscreen mode

the sum of two parameters

public class Main{
  static int myMethod(int x, int y) {
    return x + y;
   }
 public static void main(String[] args) {
   System.out.println(myMethod(5,7));
  }
}
outputs
12(5+7)
Enter fullscreen mode Exit fullscreen mode

It is highly recommended to store the resuly in a variable

public class Main{
  static int myMethod(int x, int y) {
    return x + y;
   }
 public static void main(String[] args) {
   int z =myMethod(5,3);
   System.out.println(z);
  }
}
outputs
12(5+7)
Enter fullscreen mode Exit fullscreen mode

If...Else in Methods

public class Main{
//create a checkAge() method with an integer variable
  static void checkAge(int age){
    if (age< 18) {
         System.out.println("Access Denied");
   } else{
      System.out.println("Access granted");
    }
}
public static main(String[] args) {
  checkAge(20);
}
output
"Access granted"
Enter fullscreen mode Exit fullscreen mode

Method Overloading
Method overloading means that multiple methods can have the same name with different parameters:

public class Main {
  static int plusMethod(int x, int y) {
    return x + y;
  }

  static double plusMethod(double x, double y) {
    return x + y;
  }

  public static void main(String[] args) {
    int myNum1 = plusMethod(8, 5);
    double myNum2 = plusMethod(4.3, 6.26);
    System.out.println("int: " + myNum1);
    System.out.println("double: " + myNum2);
  }
}

output
int: 13
double: 10.5599
Enter fullscreen mode Exit fullscreen mode

Java Scope
the scope means that variables are only accessible inside the region they are created
Method Scope
Variables declared directly inside a method are available anywhere in the method following the line of code in which they are declared:

public class Main {
  public static void main(String[] args) {

    // Code here cannot use x

    int x = 100;

    // Code here can use x
    System.out.println(x);
  }
}

Enter fullscreen mode Exit fullscreen mode

Block Scope
A block of code refers to all of the code between curly braces {}. Variables declared inside blocks of code are only accessible by the code between the curly braces, which follows the line in which the variable was declared:

public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    { // This is a block

      // Code here CANNOT use x

      int x = 100;

      // Code here CAN use x
      System.out.println(x);

   } // The block ends here

  // Code here CANNOT use x

  }
}

Enter fullscreen mode Exit fullscreen mode

Java Recursion
Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.

public class Main {
  public static void main(String[] args) {
    int result = sum(10);
    System.out.println(result);
  }
  public static int sum(int k) {
    if (k > 0) {
      return k + sum(k - 1);
    } else {
      return 0;
    }
  }
}
output
55
Enter fullscreen mode Exit fullscreen mode

example explained
When the sum() function is called, it adds parameter k to the sum of all numbers smaller than k and returns the result. When k becomes 0, the function just returns 0. When running, the program follows these steps:

10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
Enter fullscreen mode Exit fullscreen mode

Top comments (0)