Java Methods and Functions

18-Sep-2024

Learn how to define and call methods in Java programs to structure your code efficiently.


In Java, a method is a block of code that can perform certain tasks. It differs from other codes to perform a specific task. Therefore it has no effect on other codes.

A method is an independent code block. It can be used again and again. You can pass data into a method  (passing data is called a parameter). You can also pass multiple data.

the interesting part of the method is after declaring or creating it must be call. If you never call the method, the method will never execute.

Methods are known as functions in other programming languages

Declaring and calling a method must follow certain rules


access_modifier return_type method_name( parameters ){

// method body perfor your task

}


1. access_modifier:  It defines how the method can be accessed. here different type of access modifier have. for example public, private protected.

For public, it is accessible in all classes.

and for private, it is accessible only within the class in which it is defined. we will learn about access modifer later.

2. return_type: What type of value will the method return when it finishes its work? The data type of the value should give like int, String, etc. 

If the method return nothing then the return type will void

3. method_name: Name a method as you named the variable earlier. And follow the naming rules.

4. parameters: It contains the data type and variable name. parameters should give separated by comma.

5. method body: It is enclosed with curly braces. you can perform your task here.


Example:



public void sumWithoutReturn(int a, int b){
int sum = a + b;
System.out.println(sum);
}


public int sumWithReturn(int a, int b){
int sum = a + b;
return sum;
}


Here two example are given. In the first example the method return nothing and the second example return a int value;


Types of method:

 1. Predefined method

 2. User-defined method


 1. Predefined method

predefined methods are already defined in java class libraries . It is also known as built-in method.

for example random, max, etc methods.



double randomNum = Math.random();
System.out.println(randomNum);


this random method give every time a new number. random method are not define by us. it is already defined in java class libraries.



int a = 10, b = 20;
int max = Math.max(a, b);
System.out.println(max); // output is 20


this max method give the max number.


 2. User-defined method


The methods we declare are called user defined methods. We already learn how to declare a user defined method with example.


Static method:

static method are declare with the keyword static. It is a class method. We can access static method without creating the class object. The main method is a static method. 

example of static method



public static void summation(int a, int b){
// method body
}



Non-static or instance method:

for non static method, it is must to create an object of its class before calling the method.

example



public void summation(int a, int b){
// method body
}



Comments