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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago