Learn how to create and define method in Java language and how to call methods in java program. Also, learn defining multiple methods and why to use methods in java with example and exercises.

Syntax of method in Java

modifier returnValueType methodName(list of parameters) {
			 // Method body;
			//Group of statements
			}

In below example,

“public” is modifier

“Void” is return type

Orange () – Orange is method name, () can be empty or can have list of parameters

public static void orange() {

		System.out.println("This is Orange!!!");
	}

We will read modifier and return types and about “public static void” in details later.

For now, lets focus on method with empty parameter and void return type, void means, method returns nothing.

Method in java always has bracket () after the method name with / without parameters and must have return type.

Example of Creating and Defining a Method in Java

This is how we create and define a method in a java program. Notice the bracket () after the method named orange or after main method.

In below example, main() and orange() are methods. They are not returning anything. So, preceded with “void” return type.

Focus on method and its body. It does not have semi colon; after method name e.g. orange (); but orange () {}

public class Sample {

	public static void main(String[] args) {

		
	}

	// Defining a method
	public static void orange() {

		System.out.println("This is Orange!!!");
	}

}

How to call a method in Java?

As an example, in main() method, we are calling orange() method using syntax orange();

public class JavaMethods {

	public static void main(String[] args) {

		// This is how we call a method
		orange();

	}

	// Defining a method
	public static void orange() {

		System.out.println("This is Orange!!!");
	}

}

IN BRIEF,

Method has modifiers like private and public etc. to restrict access of methods from outside of the class.

Return type shows what value a method is returning. If it does not return anything, then we write “void” keyword before method. If it returns int value then return type will be “int”, if it returns String value then, return type will be “String”.

Note that we will discuss “public static void” , meaning modifiers and return type in detail later.

WARNING:

We CANNOT define a method inside another method. But, we can call a method from any method.

For example, Below, apple() method definition is wrong as it has been defined inside the orange() method.

public class Sample {

	public static void main(String[] args) {

	}

	// Defining a method
	public static void orange() {

		System.out.println("This is Orange!!!");
		
		public static void apple() {

			System.out.println("This is Orange!!!");
		}
	}

}

Correct way to do this is :

public class Sample {

	public static void main(String[] args) {

	}

	// Defining a method
	public static void orange() {

		System.out.println("This is Orange!!!");
		
		
	}
	
	public static void apple() {

		System.out.println("This is Orange!!!");
	}
	

}

Defining multiple methods in java

We can create and define multiple methods. In below example, two methods orange () and apple () has been defined and called in main () method.

Notice closely, how methods have been defined and called in main() method.

public class JavaMethods {

	public static void main(String[] args) {

		orange();
		apple();
		
	}
	
	//defining methods
	public static void orange() {

		System.out.println("This is Orange!!!");
	}

	
	public static void apple() {

		System.out.println("This is apple!!!");
	}
}

NOTE: You can call a method from any other method.

Why to use Methods in Java?

We use method for readability and maintainability of an application.

#Bad Code

public class JavaMethod2 {

	public static void main(String[] args) {
		System.out.println("Garlic");
		System.out.println("Curd");
		System.out.println("Paneer");
		System.out.println("Orange");
		System.out.println("Banana");
		System.out.println("Butter");
		System.out.println("Milk");
		System.out.println("Lassi");
		System.out.println("ButterMilk");
		System.out.println("Mango");
		System.out.println("Apple");
		System.out.println("Potato");
		System.out.println("Onion");
		System.out.println("Tomato");
		
		

	}
}

#Good Code

public class JavaMethod2 {

	public static void main(String[] args) {
		fruitShop();
		vegShop();
		dairyShop();

	}

	public static void fruitShop() {
		System.out.println("\nFRUITS");
		System.out.println("Mango");
		System.out.println("Apple");
		System.out.println("Orange");
		System.out.println("Banana");

	}

	public static void vegShop() {
		System.out.println("\nVEGETABLES ");
		System.out.println("Potato");
		System.out.println("Onion");
		System.out.println("Tomato");
		System.out.println("Garlic");

	}

	public static void dairyShop() {
		System.out.println("\nDAIRY");
		System.out.println("Curd");
		System.out.println("Paneer");
		System.out.println("Butter");
		System.out.println("Milk");
		System.out.println("Lassi");
		System.out.println("ButterMilk");
		

	}
}

Exercises on methods in Java

Exercise-1: A person running and walking

Create methods run() and walk() in a class Person. Using Java system.out.println method print “running…” and “walking…” in the methods run and walk respectively.

Print an string “A person is” in main() method and call run and walk method in the main() method.

Run the program. The output should be as below

A person is

runing…
walking…

Exercise-2: Creating a fruit store using methods.

Create methods for each fruit named orange, banana and mango. Print their name inside the respective methods body.

Create a method name fruitStore. Print statetment as “FruitStore…” inside it, and call all fruit methods you have created inside the fruitStore. Call fruitStore method inside the main method.

Output should be as below:

FruitStore…

Orange
Banana
Mango

Related Posts