Methods in java

(Last Updated On: February 24, 2019)

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

 

In below example,

“public” is modifier

“Void” is return type

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

 

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 () {}

 

 

How to call a method in Java?

As an example, in main() method, we are calling orange() method using syntax 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.

 

Correct way to do this is :

 

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.

 

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

 

#Good Code

 

 

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