It’s time to create a method
- Method overview.
- Method arguments.
- Method return type.
Method overview
A method is a block of code that can be executed when it’s called by its name. The methods are actions in the program. Also, it’s useful to think that method is a separate program that has input, process, and output.
The core idea of creating methods is reusability. We can create a method once and reuse it many times. However, be careful to create extremely generic methods. These kinds of methods are messy and hard to read. The method should solve only one problem.
In OOP methods are behaviors of the objects and they usually manipulate with object properties.
In the above example, we created our method printHello
and used it in the main method. The output of this code is Hello!
.
Let’s break down our method
public static void printHello() {
System.out.println("Hello!");
}
public
— access modifier. it will define from where we can use this method. Public methods can be used from anywhere in the project.
static
— it means this method belongs to the class and not to a specific object. In this case, I made it static to use directly in the main method.
void
— it’s the return type of our method. Methods can return some data back to the place where it was called. void
means method doesn’t return anything and just performs a specific action. While creating, we need to decide what data type method will return.
printHello
— the name of the method. We call our method by its name to use it.
()
— method arguments. Here we have no arguments for our method. Arguments are used to pass data as input to our methods.
{}
— the body of the method. The actual code that will be executed goes into the body.
Method arguments
The method argument is the way we pass our input data to our method. In our first example, the printHello()
method didn’t have arguments. So when using it, we didn’t have to pass any data.
In the above example, we have a method that has one String argument. We declare method arguments between parenthesis after the name.
- We have to predefine our argument data type. In our case, we have one String argument. As you can see, when we use our method we actually pass the String value. It will not compile if we pass different data types, for example
int
. - We used our method three times in the above program. And we have three different outputs. When used our method as
printHello("John");
program printedHello John
so the argumentname
will take a value"John"
. When it’s called asprintHello("Alex")
on line 7 it will printHello Alex
because we gave a different value to our method. So that’s how we pass data into our methods via method arguments. - There is no limit to the number of arguments. Good practice to keep it up to 3 or max 4 arguments for a method. If you need to pass more data you can use Array or pass them as Object. We will learn about them the future articles.
In the above example, we have a method printDetails(String fullName, int age)
that accepts two arguments.
The above code does not compile. The number of arguments and their type should match when calling our method.
In the above example, our method has one int
argument but we are passing byte
and then short
in the main method. Everything works fine because byte and short can be autoboxed to an int automatically and Java allows us to pass them to int argument. We will have the same behaviors for auto casting when working with objects which have a parent-child relationship.
Method return type
As we discussed a bit, methods can return some data back. So far we have been using only void methods. They just perform actions and do not return any data back.
In order to create a method that returns something, we need to use the method return type.
Now, we are increasing our salary by 10! This time instead of printing, we are increasing the method argument salary
by 10 and returning it.
- If you specify that method returns something then it must return. Code will not compile if you are not returning a promising data type.
- To return something we use
return
keyword. It’s important to remember that after returning, our program will exit from the method. You cannot have other statements after the return statement unless the return statement is under some condition. - We need to return the exact type that we specified as the method return type.
- We can use our method that returns something and assign the return value to the same variable type.
Let’s answer some questions:
How do I know when I need to create a separate method?
When we write a duplicate code. If you catch yourself typing something similar twice it’s might be a time to create a method. Also, methods are used as object behaviors so while designing the object you will create them.
Should I always create static
methods?
No, I just used examples with the static method to simplify the code. If a method is a behavior of a specific object, it shouldn’t be static. Usually, static methods are helper methods of util classes.
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Design Patterns
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET