Methods in Java or Java methods is a powerful and popular aspect of Java programming.
What are Methods in Java?
A method in Java is a block of code that, when called, performs specific actions mentioned in it. For instance, if you have written instructions to draw a circle in the method, it will do that task. You can insert values or parameters into methods, and they will only be executed when called. They are also referred to as functions. The primary uses of methods in Java are:
- It allows code reusability (define once and use multiple times)
- You can break a complex program into smaller chunks of code
- It increases code readability
How to Declare Methods in Java?
You can only create a method within a class. There are a total of six components included in a method declaration. The components provide various information about the method.
Below is the syntax to declare a method and its components list.
public int addNumbers (int a, int b){
//method body
}
Access specifier:
It is used to define the access type of the method. The above syntax sees the use of the “public” access specifier. However, Java provides four different specifiers, which are:
- Public: You can access it from any class
- Private: You can access it within the class where it is defined
- Protected: Accessible only in the same package or other subclasses in another package
- Default: It is the default access specifier used by the Java compiler if we don’t mention any other specifiers. It is accessible only from the package where it is declared
ReturnType:
It defines the return type of the method. In the above syntax, “int” is the return type. We can mention void as the return type if the method returns no value.
Method name:
It is used to give a unique name to the method. In the above syntax, “addNumbers” is the method name. This tutorial looks at some tips for naming a method, shortly.
Parameter list:
It is a list of arguments (data type and variable name) that will be used in the method. In the above syntax, “int a, int b” mentioned within the parentheses is the parameter list. You can also keep it blank if you don’t want to use any parameters in the method.
Method signature:
You don’t have to do anything additional here. The method signature is just a combination of the method name and parameter list.
Method body:
This is the set of instructions enclosed within curly brackets that the method will perform.
In the above example:
- ‘Public’ is the access specifier
- The return type is ‘int’ (i.e. integer)
- The method name is addNumbers
- int x and int y are the parameters
- addNumbers (int x, int y) is the method signature
- The method body is:
{
int addition = x + y;
return addition;
}
Adding an Example would be great to increase the clarity about the concept
How to Name a Method?
Some of the rules and tips to name the methods in Java are:
- Try to use a name that corresponds to the functionality (if the method is adding two numbers, use add() or sum())
- The method name should start with a verb and in lowercase (Ex: sum(), divide(), area())
- For a multi-word name, the first word should be a verb followed by a noun or adjective without any space and with the first letter capitalized (Ex: addIntegers(), areaOfSquare)
How to Call a Method?
As mentioned earlier, you need to call a method to execute and use its functionalities. You can call a method by using its name followed by the parentheses and a semicolon. Below is the syntax for calling a method:
add();
The example below will create an example method named exMethod() and call it to print a text.
What are the Types of Methods in Java?
Methods in Java can be broadly classified into two types:
- Predefined
- User-defined
Predefined Methods
As the name gives it, predefined methods in Java are the ones that the Java class libraries already define. This means that they can be called and used anywhere in our program without defining them. There are numerous predefined methods, such as length(), sqrt(), max(), and print(), and each of them is defined inside their respective classes.
The example mentioned below uses three predefined methods, which are main(), print(), and sqrt().
User-defined Methods
Custom methods defined by the user are known as user-defined methods. It is possible to modify and tweak these methods according to the situation. Here’s an example of a user-defined method.
Methods in Java can also be classified into the following types:
- Static Method
- Instance Method
- Abstract Method
- Factory Method
Creating Static Methods in Java
Static methods are the ones that belong to a class and not an instance of a class. Hence, there is no need to create an object to call it, and that’s the most significant advantage of static methods. It is possible to create a static method by using the “static” keyword. The primary method where the execution of the Java program begins is also static.
Please add the code manually by typing
Applying Instance Methods in Java Code
The instance method is a non-static method that belongs to the class and its instance. Creating an object is necessary to call the instance method.
Instance methods are further divided into two types:
-
Accessor Method
It is used to get a private field’s value, accessor methods in Java can only read instance variables. They are always prefixed with the word ‘get’.
-
Mutator Method
It is used to get and set the value of a private field, mutator methods in Java can read and modify instance variables. They are always prefixed with the word ‘set’.
Using Abstract Methods in Java
Abstract methods in Java do not have any code in them. This means that there is no need to provide the implementation code while declaring it. Instead, it is possible to declare the method body later in the program. It is known that one can declare an abstract method by using the “abstract” keyword. There is another hard rule to declare abstract methods, and it is that they can only be declared within an abstract class.
Factory Method
Factory methods are the ones that return an object to the class where it belongs. Usually, all static methods also fall into this type of method.
Don't miss out on the opportunity to become a Certified Professional with Simplilearn's Post Graduate Program in Full Stack Web Development. Enroll Today!
Conclusion
In this tutorial, you were introduced to everything about methods in Java. It’s time to implement your learning and do some practicals. You can also refer to our methods and encapsulation tutorial to delve deeper into this concept. This tutorial will help you learn how to apply the OOPs concepts of access modifiers and encapsulation to Java classes. It will enable you to get a strong grasp on Java programming. But if you want to excel in overall Java development, Simplilearn’s Post Graduate Program in Full Stack Web Development is the best way to do so. The 60 hours of applied learning and 35 coding-related exercises make the course adept at helping you become an expert developer. You will work on the most popular Java frameworks, including Hibernate and Spring during this course. Have any questions for us? Leave them in the comments section, and our experts will get back to you sovon. Happy learning!