5-Java Methods
5-Java Methods
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:
// code to be executed
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;
In the following example, myMethod() is used to print a text (the action), when
it is called:
Example
Inside main, call the myMethod() method:
myMethod();
Run example »
Example
public class MyClass {
}
public static void main(String[] args) {
myMethod();
myMethod();
myMethod();
Parameters are specified after the method name, inside the parentheses. You
can add as many parameters as you want, just separate them with a comma.
The following example has a method that takes a String called fname as
parameter. When the method is called, we pass along a first name, which is
used inside the method to print the full name:
Example
public class MyClass {
myMethod("Liam");
myMethod("Jenny");
myMethod("Anja");
// Liam Refsnes
// Jenny Refsnes
// Anja Refsnes
Run example »
Multiple Parameters
You can have as many parameters as you like:
Example
public class MyClass {
myMethod("Liam", 5);
myMethod("Jenny", 8);
myMethod("Anja", 31);
// Liam is 5
// Jenny is 8
// Anja is 31
Run example »
Note that when you are working with multiple parameters, the method call
must have the same number of arguments as there are parameters, and the
arguments must be passed in the same order.
Return Values
The void keyword, used in the examples above, indicates that the method
should not return a value. If you want the method to return a value, you can
use a primitive data type (such as int, char, etc.) instead of void, and use
the return keyword inside the method:
Example
public class MyClass {
return 5 + x;
System.out.println(myMethod(3));
// Outputs 8 (5 + 3)
Run example »
Example
public class MyClass {
return x + y;
}
public static void main(String[] args) {
System.out.println(myMethod(5, 3));
// Outputs 8 (5 + 3)
Run example »
Example
public class MyClass {
return x + y;
System.out.println(z);
// Outputs 8 (5 + 3)
Run example »
Example
public class MyClass {
} else {
Exercises
Exercise:
Add a fname parameter of type String to myMethod, and output "John Doe":
static void myMethod( ) {
System.out.println( + " Doe");
}
Video Lecture
https://www.youtube.com/watch?v=bzYt3JCmpVUhttps://www.youtube.com/watch?
v=bzYt3JCmpVUhttps://www.youtube.com/watch?v=bzYt3JCmpVUhttps://www.youtube.com/
watch?v=bzYt3JCmpVUhttps://www.youtube.com/watch?v=bzYt3JCmpVUBottom of Form