Categories: Java

Methods in Java

A method is a block of code, a group of statements, or a set of code that performs a specific task or operation. These are also known as functions in other programming languages.

  • The method describes the behavior of an object.
  • It increases code reusability.
  • It also provides an easy modification of the code.
Method Declaration:

Method attributes such as visibility, return type, name, and arguments are all listed in the method declaration.

  • A method signature can be found in every method. It’s included in the method definition. It contains the method name as well as a list of parameters.
  • The method’s access specifier or modifier is the method’s access type. It specifies the method’s visibility. There are four different types of access specifiers in Java:
ModifierClassPackageSubclassGlobal
PublicYesYesYesYes
ProtectedYesYesYesNo
DefaultYesYesNoNo
PrivateYesNoNoNo
  • The return type of a method is the data type it returns. It could be a primitive data type, an object, a collection, or void, for example. The void keyword is used when a method does not return anything.
  • The name of a method is defined by its method name, which is a unique name. It must be appropriate for the method’s functionality.
  • The parameter list is a collection of parameters separated by a comma and enclosed in parentheses.
  • The method declaration includes the method body. It contains all the actions that must be completed. It is protected by a pair of curly braces.
Syntax to declare the method in Java:
Access_specifier return-type methodName(parameter-list) //method header
{
 //method-body  method signature
}
Example:
public String getDetails(String st, int age, float per)
{
 String details=details+" "+st+" "+a+" "+per;
 return details;
}
Calling a Method:

In a program, calling a method is straightforward. The program control passes to the called method when we call or invoke a user-defined method.

for instance,

String ans=getDetails("Rabecca Fatima",20,89.70);

It will return a value Rabecca Fatima 20 89.70 after appending the argument passed during the method call.

Example: Even or Odd
import java.util.Scanner;  
public class EvenOdd  
{  
public static void main (String args[])  
{  
//creating Scanner class object     
Scanner sc=new Scanner(System.in);  
System.out.print("Enter the number: ");  
//reading value from user  
int num=sc.nextInt();  
//method calling  
findEvenOdd(num);  
}  
//user defined method  
public static void findEvenOdd(int num)  
{  
//method body  
if(num%2==0)   
System.out.println(num+" is even");   
else   
System.out.println(num+" is odd");  
}  
}  
Output:

By providing different inputs, we get different values accordingly.

Enter the number: 34
34 is even
Enter the number: 91
91 is odd
Types of Method

There are two types of methods in Java:

  • Predefined Method
  • User-defined Method
Predefined method:
  • Predefined methods are methods that are already defined in the Java class libraries.
  • It’s also known as the built-in method or standard library method. We can use these methods directly by calling them at any point in the program.
  • length(), equals(), compareTo(), sqrt(), and other pre-defined methods are examples.
User-defined method:
  • A user-defined method is a method created by the user or programmer.
  • These methods are modified according to the requirement.
  • The even-odd program is an example of the user-defined method.
call-by-reference and call-by-value


An argument can be passed to a method in two ways.

  • call-by-value: In this approach, a method is passed a copy of an argument value. Inside the method, changes to the argument value have no effect on the arguments.
  • call-by-reference: A method is called by passing a reference to an argument. The argument value will be affected by any changes made within the method.

Important: Java is Strictly Pass by Value

Note: also read about the OOPs in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago