What is a method in java?
Method is a collection of executable java statements. In other words, method acts as a container for different java statements. In java, methods mainly perform the operations on data. Methods are
- The advantage of creating a method is code re-usability which means write once and execute multiple times.
- Methods make modifications easy.
- It makes code easy to read and understand.
- Creating methods reduces boiler-plate code
Types of Methods in Java
Methods are mainly classified into two types: Pre-defined( Internal) and User-defined (External) methods
Predefined Methods:
- Predefined methods (internal methods or inbuilt methods) are already declared in java
- JVM automatically executes the internal methods
- Developer cannot make any changes into internal methods.
User defined Methods:
- If a developer creates a method then it is an external methods or user defined method.
- Developer can create any number of external methods.
- By creating external methods we develop modular code(source code which is easy to maintain and test).
Declaration of methods in Java
Method declaration is done within a class. Methods are declared using multiple components. Generally, the components in a method are method signature and method body. Method body consists of executable java statements and Method signature consists of the following components:
-
Access modifier:
- Access modifiers are used to apply visibility restrictions on members of the class.
-
Non access modifier:
- Non-access modifiers change the behavior of member but it will not affect the visibility of that method.
- Following are the non-access modifiers in Java.
- static
- abstract
- final
-
Return type:
- Return type in java indicates the type of value which a method returns.
- Void method:
- If the method is having return type as void then we cannot transfer the data from that particular method.
- Non void method:
- If the method contains the return statement then it will be considered as non void method.
- It is possible to transfer the data from one method to another method with the help of return statement.
- If the method is having return statement then we have to use appropriate datatype as a return type.
- Return statement should be the last statement of method body.
-
Method name:
- Method declaration also consists of method name. Method naming is done using any identifier.
- As java supports method overloading and method overriding, multiple methods can share same name.
-
Parameters:
- Parameterized method:
- The method which accepts input parameter is a parameterized method.
- Number of arguments should be same as that of the number of parameters at the time of method call.
-
Example for Parameterized Method
class MethodDemo{ public static void main(String[] args){ addition(10,20,30); } //external method public static void addition(int a,int b, int c){ System.out.println("Addition :"+(a+b+c)); } }
- Non parameterized method:
- The method which does not accept input parameters is a non parameterized or zero parameterized method.
- We cannot pass any arguments to the non parameterized method.
-
Example for Non Parameterized method
//Calculate Area of Circle class MethodDemo{ static double radius=12.0; public static void main args(String[] args){ calculateArea(); } public static void calculateArea(){ double area=3.14*radius*radius; System.out.println("Area of Circle is : "+area); } }
- Parameterized method:

