Method
A method is just like a function in C or C++, it contains a set of instructions that are coded to perform some task. It allows you to change the code within methods. We will learn about the method in detail.
A method in Java is a code block that performs some specific tasks and enables you to reuse the code within this section. You can use the methods from other classes by importing the package. It is the main part of Java as the code resides within it. There are many predefined methods that allow you to use the java features in your program.
How you can declare a method
A method declaration defines details like- attributes, its visibility, return-type, arguments and the name. Below are the used components of the method header-
This is a method signature that contains information like method name and the parameters.
Access specifier- it specifies the visibility of the method and has four values-
- Public- accessible to all the other classes.
- Private- accessible to the same class where it is defined.
- Protected- accessible within the same package or in a subclass in other packages.
- Default- if no visibility criteria are mentioned then it is considered to be the default, which is accessible within the same package.
Return type- it specifies the data type of the data that is returned by the method. The return type can be a primitive data type, collection, void or object. If your method is not returning any value then the method has the return type as void like the main method.
Method name- it is the name that is given to the method and it is named according to the Java naming convention. It has to be unique and should be a noun.
Parameter List- these are a list of the parameters that are being passed. The list is separated with a comma with their data type and name.
Method body- it is a section where the code resides and perform some action. The code is enclosed within the curly braces.
Types of method
Java supports two types of methods- predefined and user-defined.
Predefined methods
Java class library consists of a number of predefined methods which are also known as the built-in methods. You can directly use the predefined methods within the program at any time. There is some code associated with these methods which will run when these methods are called and perform the specified tasks. Some methods like- length(), equals(), compareTo() etc. every pre-defined method is defined within a class which is the Java library class. In some cases to use those methods, you have to import the corresponding class.
Example-
public class DemoClass
{
public static void main(String[] args)
{
System.out.print("The square: " + Math.sqrt(2));
}
}
Output-
The square: 4
In the above example, main(), print() and max() are predefined methods so there is no declaration required.
User-defined method
Apart from the predefined methods, Java allows you to create your own methods which can be modified as per the requirement.
Example-
public static void EvenOddMethod(int num)
{
//method to check odd or even
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
Above method is defined as a void which means this method will not return any data. The method is passed with a num value of int type.
How to call an user-defined method
Whenever we call the user-defined method the control will transfer to the method immediately.
Example-
import java.util.Scanner;
public class EvenOdd
{
public static void main (String args[])
{
//creating Scanner class object
Scanner scan=new Scanner(System.in);
System.out.print("Enter number: ");
//to read value from user
int num=scan.nextInt();
//method calling
findEvenOdd(num);
}
public static void findEvenOdd(int num)
{
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
}
Output-
Enter the number: 12
12 is even
What is a static method
You can define a static method with the static keyword. A static method belongs to the class instead of a class instance. You can call this method without object creation. This method will access the static data members and can change its value. You can call the static method using the class name. The main() method is the static method.
Example-
public class DisplayDemo
{
public static void main(String[] args)
{
show();
}
static void show()
{
System.out.println("static method.");
}
}
Output-
static method.
What is an instance method
If a method is not static then it is an instance method and to access the method you have to create the object of the class.
Example-
public class InstanceMethod
{
public static void main(String [] args)
{
InstanceMethod obj = new InstanceMethod();
System.out.println("The sum is: "+obj.add(12, 13));
}
int s;
public int add(int a, int b) //method has int data type
{
s = a+b;
return s; //returning int data
}
}
Output-
The sum is: 25
An instance method is of two types- accessor method and mutator method
Accessor method- it will read the instance variable and is prefixed with the word ‘get'. These methods are also known as getters which will return the private field value.
Example-
public int getId()
{
return Id;
}
Mutator method- it will read the instance variable but also change the value. These methods are prefixed with set keywords and known as setters. This method will accept the parameter of the mentioned data type but will not return anything. You can set the private field value.
Example-
public void setDemo(int num)
{
this.num = num;
}
What is an abstract method
An abstract method does not have the body. The abstract class that implements the interface will provide the body definition of that method. An ‘abstract’ keyword is used to create an abstract method.
Example-
abstract class DemoClass
{
abstract void display();
}
public class MyClass extends DemoClass
{
//implementing method
void display()
{
System.out.println("Hello");
}
public static void main(String args[])
{
DemoClass obj = new MyClass();
obj.display();
}
}
Output-
Hello