Method in Java
A method is a way to perform some task.
What is a method in Java?
A method is a block of code or collection of statements or a set of
code grouped together to perform a certain task or operation.
• A method is like a function i.e. used to expose the behavior of an
object.
• It is a set of codes that perform a particular task.
Syntax of Method:
<access_modifier> <return_type>
<method_name>( list_of_parameters)
{
//body
}
Advantage of Method:
• Code Reusability
• Code Optimization
Note: Methods are time savers and help us to reuse the code
without retyping the code.
The return type: The data type of the value returned by the method
or void if does not return a value. It is Mandatory in syntax.
Parameter list: Comma-separated list of the input parameters is
defined, preceded by their data type, within the enclosed parenthesis.
If there are no parameters, you must use empty parentheses (). It
is Optional in syntax.
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.
Types of Method
There are two types of methods in Java:
• Predefined Method
• User-defined Method
1. Predefined Method
• In Java, predefined methods are the method that
is already defined in the Java class libraries is
known as predefined methods.
• 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.
• predefined methods main(),
print(), and max().
Predefined.java
public class PreDefined
{
public static void main(String[] args)
{
// using the max() method of Math class
System.out.print("The maximum number is: " +
Math.max(9,7));
}
}
2. User-defined Method
• The method written by the user or programmer is known as a
user-defined method. These methods are modified according to
the requirement.
Ways to Create Method in Java
There are two ways to create a method in Java:
1. Instance Method: Access the instance data using the object
name. Declared inside a class.
2. Static Method: Access the static data using class name. Declared
inside class with static keyword.
Instance Method: Static Method:
Syntax: Syntax:
// Instance Method //Static Method
void method_name(){ static void method_name(){
body // instance area body // static area
}
}
Method Calling
The method needs to be called for use its functionality. There can be three
situations when a method is called:
A method returns to the code that invoked it when:
• It completes all the statements in the method.
• It reaches a return statement.
• Throws an exception.
Example of Method :
class Main {
// create a method
public static int square(int num) {
// return statement
return num * num;
}
public static void main(String[] args) {
int result;
// call the method
// store returned value to result
result = square(10);
System.out.println("Squared value of 10 is: " + result);
}
}
Representation of the Java method returning a
value
Example Method Parameters
class Main {
public static void main(String[]
args) {
// method with no parameter
public void display1() {
// create an object of Main
System.out.println("Method
Main obj = new Main();
without parameter");
}
// calling method with no
parameter
// method with single parameter
obj.display1();
public void display2(int a)
{
// calling method with the
System.out.println("Method
single parameter
with a single parameter: " + a);
obj.display2(24);
}
}
}
Execute this Code in the code
editor??