Methods in Java
Methods in Java
Methods in Java are the building blocks of a Java application. In Java, a method
is a set of code used to write the logic of the applications which perform some
specific tasks or operations.
In Java programming, a method is executed when it is called from another
method. The main() method is the first method that is executed by JVM (Java
Virtual Machine) in Java program.
When a method is called, it returns a value to the calling method. It can also
perform a task without returning any value to the calling method.
We can call a method from anywhere in Java program. Each method has a single
entry point and a single exit. A method can also call itself. We know this
technique as a recursion in Java.
}
Method Declaration in Java
A method must be declared inside the class.
In general, a method has six fundamental parts such as modifiers, method name, return
type, parameter list, exception list, and body.
Method Signature in Java
The method name with parameter list (number of parameters, type of parameters, and order of
parameters ) is called method signature in Java.
Examples
1. add(int a, int b) // Number of parameters is 2, and type of parameter is int.
2. m1(String name) // Number of parameters is 1, and type of parameter is String.
3. sub(): No parameter.
Access Specifier:
Access specifier or modifier is the access type of the method. It specifies the visibility of the method.
Java provides four types of access specifier:
Public: The method is accessible by all classes when we use public specifier in our application.
Private: When we use a private access specifier, the method is accessible only in the classes in which it is
defined.
Protected: When we use protected access specifier, the method is accessible within the same package or
subclasses in a different package.
Default: When we do not use any access specifier in the method declaration, Java uses default access
specifier by default. It is visible only from the same package only.
Return Type:
Return type is a data type that the method returns. It may have a primitive data type, object,
collection, void, etc. If the method does not return anything, we use void keyword.
Method Name:
It is a unique name that is used to define the name of a method. It must be corresponding to
the functionality of the method. Suppose, if we are creating a method for subtraction of two
numbers, the method name must be subtraction(). A method is invoked by its name.
Parameter List:
It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It
contains the data type and variable name. If the method has no parameter, left the parentheses blank.
Method Body:
It is a part of the method declaration. It contains all the actions to be performed. It is
enclosed within the pair of curly braces.
Naming a Method:
While defining a method, remember that the method name must be a verb and start with
a lowercase letter.
Types of Method:
There are two types of methods in Java:
Predefined Method
User-defined Method
Predefined methods:
Predefined methods in Java are those methods that are already defined in the Java API (Application
Programming Interface) to use in an application.
It is also known as the standard library method or built-in method.
We can directly use these methods just by calling them in the program at any point.
Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc.
Each and every predefined method is defined inside a class. Such as print() method is defined in
the java.io.PrintStream class.
Example:
}
}