How to Call a Method in Java?
Learn via video course

Overview
A method in Java is a group of statements that carry out a certain action or function. It is commonly used because it enables code reuse, which refers to the ability to write code once and use it multiple times.
To call a method in Java, you need to follow these basic steps:
- Define the method: This includes giving the method a name, specifying any arguments that it takes, and defining what the method should do when it is called.
- Create an instance of the class: If the method you want to call is part of a class, you need to create an instance of that class.
- Call the method: Once you have an instance of the class (if necessary), you can use the name of the method, followed by any arguments that the method takes (if any) to call the method.
Scope
- The article explains the concept of methods in Java.
- The article covers the definition, syntax, and types of methods in Java, including predefined methods and user-defined methods. It also explains the concept of static and abstract methods in Java. T
- he scope of the article is limited to the basic understanding of methods in Java and does not cover advanced topics or complex applications of methods.
What is a Method?
In Java, a method is a block of code that performs a specific task. Methods are used to encapsulate functionality and make code more modular, reusable, and easier to read and maintain.
A method is defined within a class and can be called from within the same class or from other classes, depending on its access modifier. Methods can have zero or more input parameters and may return a value, which can be of any data type, including primitive types and objects.
Methods in Java are typically used to perform operations on objects or to manipulate data, and can be used to accomplish a wide range of tasks, from simple calculations to complex algorithms. By using methods, programmers can break down complex problems into smaller, more manageable pieces, which can be easier to understand and maintain.
Syntax
Types of Methods
There are two types of methods in Java:
Predefined Methods in Java
Predefined methods in Java refer to the methods that are already defined in the Java class libraries. It is also known as the standard library method or built-in technique. These methods can be used immediately by simply calling them within the application at any point.
Among the pre-defined methods are sqrt(), compareTo(), length(), and equals(). When we invoke any of the predefined methods in our program, a sequence of background codes relevant to that method that are already present in the library are executed.
In this example we are going find how to call a method in java of a predefined type. In this example we have used max() which is defined in the Math class
Output
Code Explanation
- The three predefined methods main(), print(), and max() were utilized in the example above.
- Because they are predefined, we have used these methods directly without declaring them.
- The PrintStream class's print() method prints the outcome to the console. The Math class has a method called max() that returns the larger of two values.
User-defined Methods in Java
In Java, a user-defined method is a method that is created by the programmer (as opposed to a built-in method that is provided by Java).
A user-defined method can be used to encapsulate a specific task or operation within a program, making it easier to read, write, and maintain the code.
Example:
Output:
Explanation:
- The code above is an example of a Java program that defines a user-defined method called addNumbers().
- The main() method is the entry point of the program, and it is where the program execution begins.
- The main() method calls the addNumbers() method and passes two integer values (2 and 3) as arguments. The result of the method call is stored in the result variable.
Static Methods in Java
A static method in Java is one that is used without first constructing an object of the class in which it is defined. Static methods are any methods with the keyword static before the method name. We can invoke a static method using the class name as shown:
The best example of the static method is the main() method. It is called without creating the object of the Main class. In this example we are going to find how to call a method in java of static type. Code
Output:
Code Explanation:
The static function min() of the Math class, which returns the lower of two values, is called in the program above.
Abstract Methods in Java
In Java, an abstract method is a method that is declared but does not have an implementation in the class where it is declared. The abstract method definition ends with a semicolon instead of a method body.
An abstract method is defined using the abstract keyword before the method declaration. For example:
In this example we are going to find how to call a method in java of abstract type.
Code:
Output
Code Explanation
- In this example, we have an abstract class Shape that defines an abstract method getArea().
- The Circle class extends Shape and implements the getArea() method to calculate the area of a circle.
- In the Main class, we create an instance of Circle and assign it to a variable of type Shape.
- We can do this because Circle is a subclass of Shape. We then call the getArea() method on the Shape object, which calls the overridden getArea() method in the Circle class to calculate the area of the circle.
Conclusion
- A method in Java is a group of statements that carry out a certain action or function. It enables code reuse, which means writing code once and using it multiple times.
- To call a method, you need to define it, create an instance of the class (if needed), and call the method using the name and any arguments it takes.
- There are two types of methods in Java: predefined and user-defined. Predefined methods are already defined in the Java class libraries, while user-defined methods are created by the programmer.
- Predefined methods can be used immediately by simply calling them within the application. User-defined methods are used to encapsulate a specific task or operation within a program.
- A static method in Java is used without first constructing an object of the class in which it is defined. It can be invoked using the class name followed by the method name. The main() method is an example of a static method.