Types of Methods
-
Build-in Methods: These are predefined methods in the Java library and do not need to be created. Some examples are the max() ways present, which take two inputs and return the one greater than the other.
- User-Defined Methods: These types of methods are needed to be created by the developer.
Components Required to Create Java Methods
Access Modifier
In Java, there exist four types of access modifiers.
-
Public: These types of modifiers came up with the maximum accessibility. These can be used in the same class and within the same package. These are also accessible outside the package and also in the subclass of the outside package.
public void msg(){
System.out.println(“Hello Coders”);
}

You can also try this code with Online Java Compiler
Run Code
2. Protected: If a method is declared as protected then these can be accessed within the class along with in the same package and also outside the package by subclass only. These are not accessible outside the package.
protected void msg() {
System.out.println(“Hello Coders”);
}

You can also try this code with Online Java Compiler
Run Code
3. Private: These types of modifiers can be accessed within the same class. These are not accessible to other classes within the same package and outside the package of the subclass too.
private void msg() {
System.out.println(“Hello Coders”);
}

You can also try this code with Online Java Compiler
Run Code
4. Default: If you don’t mention any specifier, then this will be treated as the default one. This default modifier is accessible only inside the same package not outside the package. It provides more accessibility than private.
void msg(){
System.out.println(“Hello Coders”);
}

You can also try this code with Online Java Compiler
Run Code
Return Type
This consists of the data type of the value which can be returned from the function. If the return type consists of the void, it does not return anything.
Method Name
This refers to the method's name, either unique or non-unique. It should follow camel case notation.
Parameters List
This list includes all the input parameters separated by commas, along with their data types.
Exceptions
This may throw one or more than one exception. We can use more than one exception separated by commas. To learn more about exceptions, you can visit Exception Handling.
Method Body
This represents a block of code enclosed between the braces. This body contains one or more logical statements to perform the task.
You can also read about the topic of Java Destructor and Hashcode Method in Java.
Examples
Code 1
public class Ex14 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Square root of 16: " +Math.sqrt(16));
}
}

You can also try this code with Online Java Compiler
Run Code
Output
The square root of 16: 4.0

You can also try this code with Online Java Compiler
Run Code
In this code, we have used an in-built method called sqrt. This method is used to find the square root of any number. The modifier here is used is public to call the square root function.
Code 2
public class Ex15 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num1 = 10, num2 = 5, maxValue;
maxValue = Math.max(num1,num2);
System.out.println("Max value: " +maxValue);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Max value: 10

You can also try this code with Online Java Compiler
Run Code
In this code, we have used an inbuilt function of getting the maximum number of the given two numbers. The modifier here is used is public to call the maximum function.
Also check out Addition of Two Numbers in Java here.
Code 3
public class Ex16 {
int a=20;
static int c = 40;
static int d = 50;
void m1()
{
System.out.println("Third number c = " +c);
System.out.println("Fourth number c = " +d);
}
static void multiply()
{
int mNum = c*d;
System.out.println("Multiplication: " +mNum);
}
public static void main(String[] args)
{
Ex16.multiply();
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Multiplication: 2000

You can also try this code with Online Java Compiler
Run Code
In this code example, we have used a user-defined function to calculate the multiplication of two numbers. We have created a function called multiply, and then we have called that function by creating an object of that class. The modifier here is used is public to call the user-defined multiply function.
Try this code by yourself on Online Java Compiler.
Must Read Conditional Statements in Java
Frequently Asked Questions
-
What is the signature of the method in Java?
A method declaration can also be part of the method declaration in Java. It is the combination of the method name and the parameter list. The name method can be unique or non-unique.
-
What is the method hiding in Java?
Method hiding can be defined as any hierarchical structure in Java. A child class, when expressed as static bearing the same signature in the parent class, then the child's method hides the one on the parent class.
Key Takeaways
In this blog, we have covered an introduction to methods in Java. We have also covered the definition of methods along with their syntax. We also talked about types of methods. We also discussed different components required to implement a method in Java. We took several examples of codes with inbuilt and user-defined methods.
If you want to learn and practice more on this topic, please jump to our website, Guided Paths, and for participating in live contests to brush up on your skills Contests.