Java Methods – Detailed Explanation with Example

In this article, we will study and understand the working of Java methods, how to define a Java method and use them in your program with the help of examples. We will also understand –

  • Types of Methods
    • Standard Library Methods
    • User-defined Methods
  • Method Creation/Declaration
  • Calling a Method
  • Example: Method Accepting Arguments & Returning Value
  • Advantages of Using Methods

A Java method is a collection of statements that are grouped together to perform an operation. The method may or may not return any value back. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class which is different from languages like C, C++ and Python. In object-oriented programming, method is a jargon used for function.

In general there are 2 types of Methods –

  1. Standard Library Methods
  2. User-defined Methods
Standard Library Methods

The standard library methods are built-in methods in Java that are readily available for use. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. These standard libraries come along with the Java Class Library (JCL) in a Java archive (*.jar) file with JVM and JRE.

Here’s an working example:
When you run the program, the output will be: Hello World

User-defined Method

You can create your own custom methods which have custom functionality depending upon your needs. Such methods are called user-defined methods.

Method Creation/Declaration –

Method Example – 

Java Methods structure in java programming

In general, method declarations has these components :

  • Modifier-: Defines access type of the method i.e. from where it can be accessed in your application. In Java, there 4 type of the access specifiers.
    • public: accessible in all class in your application.
    • protected: accessible within the class in which it is defined and in its subclass(es)
    • private: accessible only within the class in which it is defined.
    • default :(declared/defined without using any modifier) : accessible within same class and package within which its class is defined.
  • The return type : The data type of the value returned by the the method or void if does not return a value.
  • Method Name : the rules for field names apply to method names as well, but the convention is a little different.
  • Parameter list : Comma separated list of the input parameters are defined, preceded with their data type, within the enclosed parenthesis. If there are no parameters, you must use empty parentheses ().
  • Exception list : The exceptions you expect by the method can throw, you can specify these exception(s).
  • Method body : it is enclosed between braces. The code you need to be executed to perform your intended operations.

Method signature: It consists of method name and parameter list (number of parameters, type of the parameters and order of the parameters). Return type and exceptions are not considered as part of it.
Method Signature of above function:

How to name a Method ?: A method name is typically a single word that should be a verb in lowercase or multi-word, that begins with a verb in lowercase followed by adjective, noun….. After the first word, first letter of each word should be capitalized. For example, findSum, computeMax, setX and getX

Calling a Method

Now you defined a method, you need to use it. For that, you have to call the method. Here’s how:

Program Example –

When you call the function as shown the in the above example. The control is transferred from the main() function to the max() function. The max function performs operation i.e. checks which number is max out of the 2 parameters passed, and returns the one which is maximum out of the 2. Now as the the function reaches the last statement which is a return statement, it returns the control back to the main function while returning the maximum value (in this case 6). Now since on the LHS we have a int variable num, the value returned from the max() function which is on the RHS is assigned to the num variable on the LHS as the value returned from the max() function is also int type so both are compatible.

function call working mechanism in java

What are the advantages of using methods?
  • The main advantage is code reusability. You can write a method once, and use it multiple times. You do not have to rewrite the entire code each time. Think of it as, “write once, reuse multiple times.”
  • Methods make code more readable and easier to debug. For example, add2Numbers() method is so readable, that we can know what this method will be performing addition of 2 numbers.
Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *