Java Methods: Welcome to another new post of core java tutorial series. In this article, we are going to learn about Java methods in detail. Like how we can define the java method & how we can use java method in your java program with the help of real-time examples.
What is a Method In Java?
A method is nothing but a collection of statements which is performing some specific task and return to the caller. Sometimes a method also performs some tasks and doesn’t return anything. Also, we can use methods to reuse the same code for the multiple time that means write once and use multiple times without retyping the same. That’s why methods are time savers.
Types of Methods In Java
There are mainly two types of methods available in Java. These methods categorized according to some attributes like, Is the method is defined by a user or the method is available in the standard library.
Based on that, methods divided into two types, that is:
- Standard Library Methods
- User-Defined methods
Standard Library Method In Java
The Standard Library Methods methods are nothing but the built-in methods, which are already available inside the Java standard libraries, which come along with the Java Class Library (JCL) in a Java archive (*.jar) file with JVM and JRE.
Some Example of Standard Library Methods are:
- Print(): This method is available inside the java.io.PrintSteam package. This print(“….”) method is used to print in the console. Whatever you will write inside the quotation marks that will print inside the console.
- Sqrt (): This method belongs to the math class. It will return the square root of the given number.
User-Defined Method In Java
Similarly, some users have defined methods according to their requirements. Such types of methods are called user-defined methods. So let’s understand how we can define a user-defined method:
How to Create a user-defined Method in Java
There are mainly six components of methods that are:
A. Modifier: By using modifiers, we define the access types. That means where we can access the method. In Java, we have four different access specifiers are there:
- Public: that means we can access the method all over the application
- Protected: That means that methods are accessible within the class and in its subclasses.
- Private: That method is accessible within the class only.
- Default: If you do not define any access specifiers, then Java programming language takes default as an access specifier. These methods are accessible within the class and within the package.
B. Return Type: It represents what type of value that specific method will return, and sometimes it may be void also, that time it will not return anything.
C. Method Name: With this parameter, we are defining a method name. And by using the method name, we can access that specific method.
D. Parameter list: This Field is used to pass some values to the method. If you are giving more then one value to a method then we are using a comma to separate between two parameters. We have to mention the data type and variable name within an unclosed parenthesis. If there is no parameter is there, then we have to use empty parenthesis.
D. Method Body: This is the area between two braces. Whatever operation you need to perform, those codes or statements should be written inside the braces.
Method Syntax Java
public static int methodName(int a, int b) { // method body }
In the above syntax,
- public static − modifier
- int − return type
- methodName − the name of the method
- a, b − formal parameters
- int a, int b − list of parameters
Java Method Example
package java_Basics; public class Method_Example { public static void main(String[] args) { System.out.println("Going to Call static method myMethod()"); // method call myMethod(); System.out.println("myMethod was executed successfully!"); } // method definition private static void myMethod(){ System.out.println("Printing from inside myMethod()!"); } }
How to Name a Method ( Method Naming Convention )
Method name always a single word, or sometimes the name begins with a verb in lower case followed by an adjective, noun. There is a naming convention for methods like after the first-word first letter of words should be in the capital letter. For Example softwareTestingo, findElement
Note: Method must have a unique name within the class, but sometimes a class can have multiple methods that have the same name but different in the parameters that are called method overloading.
How to call Java Method
Suppose you have defined a method, and later, you want to use that method. So, to use that method, you need to call that method. Let’s understand how to call a method
myMethod();
This statement called myMethod(), which is declared earlier.
- While Java is executing the program code, it encounters myMethod(); in the code.
- The execution then branches to the myFunction() method and executes code inside the body of the method.
- After the execution of the code inside the method body completed, the program returns to the original state and executes the next statement.
Please write comments if you find anything incorrect, or you want to share more information about the above-discussed topic.
Leave a Reply