Methods, also members of classes, are subroutines that manipulate the data defined by a class and often provide access to that data.
Java Method Types
In Java, there are several types of methods. At the very top, you have controller methods, service methods, and what are known as getter & setter methods. Fortunately, all methods have the same declaration syntax.
Controller methods
These methods act as the control tower, if you will, directing the general flow of the application. These methods use the logic built into the service methods, as well as getters and setters, to accomplish a given task by calling these methods in a logical order. As you're getting started, you will be using the main() method as your primary controller method.
Service methods
These methods do the majority of the work. We keep most of the logic that happens on objects and data within service methods.
Getter & Setter methods
These methods do exactly as they say; they GET and SET data on an object.
How do Methods Work?
Methods are discrete units of logic. Ideally, each method has but one task. Each time you see yourself writing the same few lines of code more than once, you should put those lines of code in a method and then call that method from wherever needed.
With the exception of instance variable declaration and initialization, all executable Java code must be written inside a method.
DRY - Don't Repeat Yourself
Generally speaking, if you ever see the same 2-3+ lines of code in more than one location, you have a design flaw. If you have copied code all over your program, and then you need to change the behavior of that code, it becomes increasingly difficult to maintain, manage, and upgrade with time as you have to change code all over the place. To avoid this, instead of having duplicate code around your project, you want an example of the duplicate put inside a method - and then you call that method from all those other locations. This way, when you need to update/change it - you only have to change it in one place, and that change is reflected everywhere where that method is invoked (aka "called").
How to Write a Method?
Methods in Java contain seven key components.
Seven Components of a Method
- Access modifier
- Static/Non-static
- Return type
- Method name
- Method parameter(s) / aka "arguments"
- Exceptions
- Method body
The general syntax for declaring a new method is:
[access-modifier] [static or non-static] [return-type] [method-name] (method parameter(s) aka "arguments") {
// everything
// in here
// is called
// the method body
// a method is a code block that is enclosed
// by an opening and closing curly bracket
}
Java Method Examples
If you're new to programming, then this might be a lot of new information. Below, you're going to go through several examples to give you an idea of how this works.
Method Component Infographic
The image below shows an example method and how each part belongs to the seven components you learned above.
Here's another example method including an Exception (we'll come back to Exception shortly):
Java Method Syntax
The flow of a program begins at the main method and completes each line of code in order from top to bottom. Here's an empty main method:
public class MethodExample{
public static void main(String[] args){
//First statement
//Second statement
//Third Statement
//..
}
}
Java Method with Body
The use of methods is one of many ways to modify the flow of a program. Calling a method, sayHi, makes the program's next line of execution the first line of the method, sayHi. For example, if you wanted to call a method that prints out a message, you would write a program like the following.
class Main {
public static void main(String[] args) {
//first executable statement
System.out.println("This line is executed first");
sayHi();
// third executable statement
System.out.println("This line is executed third");
//Fourth Statement
//..
}
public static void sayHi() {
// second executable statement
System.out.println("Hello world!");
}
}
Methods are used to perform routines, organize code and program flow, allow proper access to instance variables, and more.
Summary: What are Java Methods?
- Methods are subroutines that manipulate data in a program
- Methods often create and provide data for the program
- There are three types of methods: controller, service and getter/setter
- Service methods contain most of the logic of a program
- Controller methods direct the general flow of the program
- Getter and Setter methods get and set data on the object
- The
main()method is traditionally the primary controller of an application
Seven Components of a Method
- Access modifier
- Static/Non-static
- Return type
- Method name
- Method parameter(s) / aka "arguments"
- Exceptions
- Method body
Syntax
Here's the syntax for creating a method in Java, where you can substitute the variables starting with your_ with your values.
your_access_modifier static_or_non_static your_return_type your_method_name(){
//First statement
//Second statement
//Third Statement
//..
}