Java Methods
Java Methods
• A method is a way to perform some task. Similarly, the method in Java is a collection of instructions that
performs a specific task.
• It provides the reusability of code. We can also easily modify code using methods. In this section, we will
learn what is a method in Java, types of methods, method declaration, and how to call 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. It is used to achieve the reusability of code.
• We write a method once and use it many times. We do not require to write code again and again. It also
provides the easy modification and readability of code, just by adding or removing a chunk of code.
It has six components that are known as method header, as we have shown in the
following figure.
• Method Signature: Every method has a method signature. It is a part of the method declaration. It includes
the method name and parameter list.
• 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.
Protected: 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.
• Return Type: Return type is a data type that the method returns. It may have a primitive data type, object,
collection, void, etc. If the method does not return anything, we use void keyword.
• Method Name: It is a unique name that is used to define the name of a method. It must be corresponding to
the functionality of the method. Suppose, if we are creating a method for subtraction of two numbers, the
method name must be subtraction(). A method is invoked by its name.
• Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It
contains the data type and variable name. If the method has no parameter, left the parentheses blank.
• Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed
within the pair of curly braces.
• While defining a method, remember that the method name must be a verb and start
with a lowercase letter. If the method name has more than two words, the first name
must be a verb followed by adjective or noun. In the multi-word method name, the first
letter of each word must be in uppercase except the first word.
• For example:
• It is also possible that a method has the same name as another method name in the
same class, it is known as method overloading.
User-defined 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. When we call any of the predefined methods in our
program, a series of codes related to the corresponding method runs in the background
that is already stored in the library.
• Each and every predefined method is defined inside a class. Such as print() method is
defined in the java.io.PrintStream class. It prints the statement that we write inside
the method. For example, print("Java"), it prints Java on the console.
• Example:
• Example:
• We have defined the above method named findevenodd(). It has a parameter num of
type int. The method does not return any value that's why we have used void. The
method body contains the steps to check the number is even or odd. If the number is
even, it prints the number is even, else prints the number is odd.
Once we have defined a method, it should be called. When we call or invoke a user-
defined method, the program control transfer to the called method.
Let's combine both snippets of codes in a single program and execute it.
• Addition.java
• A method that has static keyword is known as static method. In other words, a
method that belongs to a class rather than an instance of a class is known as a
static method.
• We can also create a static method by using the keyword static before the method
name.
• The main advantage of a static method is that we can call it without creating an object.
It can access static data members and also change the value of it. It is used to create
an instance method. It is invoked by using the class name. The best example of a static
method is the main() method.
• InstanceMethodExample.java