If you're starting your Java programming journey, chances are good that you find method calls a bit confusing. Sometimes they return a value, and sometimes they don't – so what gives? Why do the values you pass sometimes get modified in the calling method and other times not?
In Java, the method is a code block that contains a series of statements. A method is like a function in other programming languages like C++ or Python. In this post, we will define Java methods, discuss their types, show how to call them, and answer some frequently asked questions.
Java Methods Overview
Java methods are blocks of code that contain a series of statements and run when it is called.
Methods are also commonly known as functions. Every method has a name and the option to pass data into it with parameters. Methods can either return a value or not return anything. If a method doesn't return anything, it is called a void method. Methods are beneficial because they can be reused throughout the program. This saves time and makes the code more organized.
Java Methods Syntax
Every method has the same basic syntax. The only thing that changes is the method's name and what data it takes in as parameters.
The syntax for calling a method is:
Now that we know this syntax, let's build a basic Java method.
In the example above, the method is named main, and it is a void method. The keyword public means the method can be called anywhere in the program. The keyword static means that the method is associated with the class and not an object. The keyword void means that the method doesn't return a value.
The parameter for this method is an array of strings called args. The code inside the curly braces is the method body. This is where you would put the statements you want to run when the method is called.
Now that we have a basic understanding of how methods work, let's view the different types of Java methods.
Types of Methods in Java
In Java, there are several types of methods:
- Predefined Methods
- User-defined Methods
- Static Methods
- Instance Methods
We will discuss each of these method types in detail.
Predefined methods are already defined in the Java class library. This method is also known as the library method or built-in method. These methods are ready to be used in your program. Some examples of these methods are the println(), equals(), and length(). You can call these methods by using their names.
Every predefined method is defined inside a class. Take a look at the example below.
In the example, we were able to use the predefined methods main() and println() without declaring it first. This is because these methods are already defined in the Java class library.
User-defined methods are the methods that you create as part of your program. An example of a user-defined method is shown below.
You can call the user-defined methods the same way you call predefined methods. In the example, we created a method called printMessage(). This method prints the message "Good Morning" to the console. We can call this method by using its name.
A static method is a method that belongs to a class rather than an instance of the class. They can be called without creating an object and defined using the static keyword. Static methods are often used to create utility classes. You can call static methods by using the class name followed by the dot (.) operator. An example of how to call a static method is shown below.
In the example, we are calling the static method add() from the Math class. The Math class is a utility class that contains a set of methods for performing mathematical operations. We don't need to create an object to call the add() method. We can simply use the name of the class followed by the dot operator.
An instance method is a method that belongs to an instance of a class, not the class itself. To call an instance method, you need to create an object first. An example of how to call an instance method is shown below.
In the example, we are calling the instance method accelerate() from the Car class. We first need to create an object of type Car before we can call the accelerate() method.
How do you call a method in Java?
Now that you know the different types of methods let's look at how to call them. As we mentioned, you can call methods using their names followed by parentheses () and semicolons (;). If the method includes parameters in its declaration, you pass those parameters within parentheses () without specifying their data types.
The output of the above example is the following:
The method we defined in the main() is called four times, each time with a different argument. We can see that it returns different outputs for different names.
You can also call methods without passing in any parameters. In the example below, we are calling the static method printMessage() without passing in any parameters.
When you call a method, the code inside of the method will execute. In the example above, when we call the printMessage() method, the code inside of the method will print "Good Morning" to the console.
Now that you are familiar with calling methods in Java let's clear up some common questions you may have.
Common Questions on Calling a Method in Java
1. Why doesn't my method print a value?
When the method you defined doesn't return any value, it prints nothing. If you want the method to print something, you need to add a return statement. In the example below, we've added a return statement to the printName() method to return the name.
2. What is the difference between System.out.println and the return statement?
System.out.println prints the output on the console window. It is simply there for the user's benefit and is commonly used in debugging your program.
The return statement returns a value to the function. All functions will return a value, and if there is no return statement, it will return None. The value that is returned can be further used in the program.
3. Can we override main in Java?
No, we cannot override main in Java. The main method is a static method called when a program starts. It is not possible to override static methods in Java.
However, it is possible to overload the main method. Overloading means having multiple methods with the same name but with different signatures. The signature of a method includes the number of parameters and the types of parameters.
In the example below, we have overloaded the main method by defining two methods with the same name but different signatures. The first method takes two parameters: an int and a String. The second method takes no parameters.
When we run the program, the second main method is called because it has the correct signature for a Java program. The first main method is not called because it does not have the correct signature.
Overloading is useful for defining multiple methods with the same name but different behavior.
Hopefully, this will clear up some common questions you may have about Java methods.
Call Methods in Java: Final Thoughts
Calling methods are a fundamental part of programming in any language, so it's important to understand how it works. Try calling different methods with different parameters and return types to further your understanding of Java methods. With practice, it will become second nature.