logo codesdope

Methods in Java


Till now, we were writing our codes in the main method. In this chapter, you will learn to create your own methods.

Let's start by first looking at the main method.

public static void main(String[] args)

When we run a Java program, the main method is executed first. So to execute a code in Java, there must be a main method.

Here, public is a modifier. You will learn about modifiers in later chapters.

static means you can access this method without making any object of the class ( classes and objects will be taught later ).

void means that the method will not return anything. A method may or may not return something.

eg.- we can define a method which return the computed value after addition of two ints to us. So, in this case, the method will return an int. 'main' method is a void method, it means it returns nothing.

In this chapter, we will learn about void methods and in the next chapter we will learn about methods which returns something.

Think of a situation where you want to get the area of a rectangle printed on the screen and you want to do it many times for different rectangles. Writing the same code again and again will do the job but there is a different and easier solution.

Let's create a method to do this.

class Rect{

  public static void printArea(int x,int y){
    System.out.println(x*y);
  }

  public static void main(String[] args){
    printArea(2,4);
    printArea(4,8);
    printArea(100,23);
    printArea(1221,32);
  }
}
Output
8
32
2300
39072

Here we have defined a method named 'printArea'.

public static - As said earlier, these will be discussed later. (Keep in mind that since our 'main' method is static, so to call our method from the main method, our method must be static.)
printArea - It is the name of our method. We can give any name.
(int x,int y) - We are passing two parameters, both int, to our method. This means that whenever we will call our method, we have to pass two integers to it, as done in the 'main' method ( printArea(2,4); ) 2 and 4 are two integers we are passing. In 'printArea', these 2 and 4 will be assigned to 'x' and 'y' respectively. So, it will be like
int x = 2;
int y = 4;
You will go through the whole code. It is explained below.

So, first go to the 'main' method as it is executed first.
The first statement is printArea(2,4); .
Now, Java will look for 'printArea' in our program. As 'printArea' is defined and we have passed 2 and 4 while calling it, it matches with what is required in 'printArea' ( two ints ). So, 'x' will become 2 and 'y' will be 4.
After this, in 'printArea',
System.out.println(x*y);, it will print x*y, i.e., 2*4 on the screen. Thus the code in the body of 'printArea' has been executed. Now coming back to the second line in the 'main' method printArea(4,8);, again things will be repeated but this time 'x' and 'y' will be 4 and 8 respectively.

As you have seen, we have called 'printArea' from the 'main' method (static), therefore, our method 'printArea' must has to static.

Let's see one more example:

import java.util.*;

class Oande{

  public static void odd_even(){
    int choice = 1;
    int x;
    Scanner s = new Scanner(System.in);
  while(choice == 1){
    System.out.println("Enter a number");
    x = s.nextInt();
    if(x%2 == 0 && x>=0){
      System.out.println("even");
    }
    else if(x%2!=0 && x>=0){
      System.out.println("odd");
    }
    else{
      System.out.println("Enter positive number");
  }
  System.out.println("Want to check more numbers? Enter 1 for yes and 0 for no");
  choice = s.nextInt();
}
}

  public static void main(String[] args){
    odd_even();
  }
}
Output
Enter a number
5
odd
Want to check more numbers? Enter 1 for yes and 0 for no
0

I hope the above code must be clear to you, if not, then you need to solve problems. One thing to notice here is that we are not passing anything to our method 'odd_even()'. And yes, we can do so.
While loop will continue if 'choice' is 1 and this is what we are asking the user after checking every number.

You are only good as the chances you take.
-Al Pacino


Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.