It’s time to create a method

it’s useful to think that method is a separate program
  1. Method overview.
  2. Method arguments.
  3. 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 Main.java

In the above example, we created our method printHello and using 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 — 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.

{} — 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 Main.java

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 passing the String value. It will not compile if we pass different data type, 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 printed Hello John so the argument name will take a value "John". When it’s called as printHello("Alex") on line 7 it will print Hello 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 Main.java

In the above example, we have a method printDetails(String fullName, int age)that accepts two arguments.

Does not compile

The above code does not compile. The number of arguments and their type should match when calling our method.

in Main.java

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 not return any data back.

In order to create a method that returns something, we need to use the method return type.

in Main.java

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 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 under some condition.
  • We need to return the exact type that we specified as the method return type.
in Main.java
  • 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.

This article is part of the series of articles to learn Java programming language from Tech Lead Academy:1. Introduction to programming 
2. OS, File, and File System
3. Working with terminal
4. Welcome to Java Programming Language
5. Variables and Primitives in Java
6. Methods with Java
7. Java Math Operators and special operators
8. Conditional branching in Java
9. Switch statement in Java
10. Ternary operator in Java
11. Enum in Java
12. String class and its methods in Java
13. Loops in Java
14. Access modifiers in Java
15. Static keyword in Java
16. The final keyword in Java
17. Class and Object in Java
18. Object Oriented Programming in Java
19. OOP: Encapsulation in Java
20. Inheritance in Java
21. Abstraction in Java
22. Polymorphism in Java
23. Overriding vs Overloading in Java
24. OOP Design Principles in Java
25. Array in Java
26. Data Structures with Java
27. Collection framework in Java
28. ArrayList in Java
29. Set in Java
30. Map in Java
31. LocalDate in Java
32. Exception in Java
33. IO in Java
34. Design Patterns
35. Generics in Java
36. Multithreading in java
37. JUnit
38. Big O Notation for coding interviews
39. Top 17 Java coding interview questions for SDET

Software Developer, Java Instructor https://www.techleadacademy.io/