It’s time to create a method

it’s useful to think that method is a separate program
  1. Method overview.

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 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.
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.
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 wiht Java
7. Conditional branching, comparision and logical operators
8. Switch statemet and ternary operator
9. Enum
10. String and its methods in Java
11. Loops in Java
12. Class, Object and constructor in Java
13. Object Oriented Programming in Java
14. Encapsulation in Java
15. Inheritance in Java
16. Abstraction in Java
17. Polymorphism in Java
18. Overriding vs Overloading in Java
19. OOP Design Principles in Java
20. Array in Java
21. Data Structures with Java
22. Collection framework in Java
23. ArrayList in Java
24. Set in Java
25. Map in Java
26. LocalDate in Java
27. Exception in Java
28. IO in Java
29. Design Patterns
30. JUnit

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store