You Can Pass Data
You Can Pass Data
Methods are used to perform certain actions, and they are also known
as functions.
Why use methods? To reuse code: define the code once, and use it
many times.
Create a Method
A method must be declared within a class. It is defined with the name of
the method, followed by parentheses (). Java provides some pre-defined
methods, such as System.out.println(), but you can also create your
own methods to perform certain actions:
Example
Create a method inside MyClass:
Example Explained
myMethod() is the name of the method
static means that the method belongs to the MyClass class and not
an object of the MyClass class. You will learn more about objects and
how to access methods through objects later in this tutorial.
void means that this method does not have a return value. You will
learn more about return values later in this chapter
Call a Method
To call a method in Java, write the method's name followed by two
parentheses () and a semicolon;
Example
Inside main, call the myMethod() method:
Run example »
Example
public class MyClass {
static void myMethod() {
System.out.println("I just got executed!");
}