Methods in Java: Definition & Example

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Static vs. Non-Static Methods in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:03 What Is a Method in Java?
  • 0:30 Parameters of a Method
  • 1:30 Returning a Value
  • 4:00 Java Main Method
  • 4:22 Syntax
  • 4:59 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Sudha Aravindan

Sudha has a Doctor of Education Degree and is currently working as a Information Technology Specialist.

Java programs have methods that have a name and contain a set of statements or instructions. In this lesson, we'll learn how the Java programming language uses methods and the rules and syntax we need to employ when using them.

What Is a Method in Java?

In the Java programming language, a method is a section of the program that contains a set of instructions or code. In a Java program, similar to a cake recipe, a method has a set of instructions. When the method is called, the set of instructions within the method is executed.

public int addNumbers(int x, int y) {
 int z = 0;
 z = x + y;
 return z;

Here, the name of the method is addNumbers. When the method addNumbers is called, the code within the method is executed, and the variable z is printed.

Parameters of a Method

When following the method to make cake, the ingredients like sugar and butter are combined and processed to make the final product. Similarly, Java methods have parameters (like ingredients) or data that are inputted or passed into the method. The method uses these parameter values to do the necessary data manipulation and processing. The method then usually returns the final value after all the necessary data processing is successfully performed.

public int subtractNumbers(int m, int n) {
 int p = 0;
 p = m - n;
 return p;

In this example, m and n are parameters. The Java method subtractNumbers finds the difference between m and n and saves the result in a new variable p. The values of the parameters m and n are used to generate the new variable p that is printed out on the computer screen.

The parameters of a method are declared within parentheses following the method name. If there is more than one parameter, they are separated by commas. Both the data type and the variable name (int m, int n) are specified for the parameters.

Returning a Value

When the method to make cake is completed using the necessary ingredients, the final result is a new product that is the cake. By using the parameters that are passed into the method, the method generates a new product or result. The result returned by the method is also available for use by the Java program to which this method belongs.

public int subtractNumbers(int m, int n) {
 int p = 0;
 p = m - n;
 return p;

In this example, the variable p is returned by the method. The return statement is a Java keyword return followed by the variable name.

return p;

When the method is declared, the return type of the variable is listed just before the name of the method. Here, the name of the method is subtractNumbers, and the data type of the variable being returned, p, is int (or integer), so the method declaration states:

public int subtractNumbers(int m, int n)

where int is the return type of the method subtractNumbers.

In Java, a method can return only one variable at a time. For example, the following code will generate an error:

public int int subtractNumbers(int m, int n) {
 int p = 0;
 p = m - n;
 q = m + n;
 return p;
 return q;

Here, there are the return types of 2 variables (int int) listed before the name of the method, subtractNumbers. There are 2 return statements to return p and q. However, methods in Java can return only one value at a time. To return multiple values, you would need to use an array.

Methods that Do Not Return a Value

Now, let's look at methods that do not return a value. Some Java methods do not return a value; they process a set of tasks without a return value. When a method does not return a value, it is indicated by the keyword void before the name of the method.

public void subtractNumbers(int m, int n) {
 int p;
 p = m - n;

This method does not return a value. Instead, it only prints the result of m-n on the screen. We can tell that this method does not return a value because:

  1. It has no return statement
  2. The method name (subtractNumbers) is preceded by the keyword void

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account