Get startedGet started for free

Creating your own methods

1. Creating your own methods

So far, we've talked about how Java provides built-in methods, but what if we need a method that doesn't exist yet? That's where custom methods come in.

2. Why we use methods

A method is useful when we have a task that we need to repeat. Instead of writing the same lines of code in multiple places, we can design a method once and call it whenever we need it. This keeps our code cleaner, makes it easier to update or fix mistakes, and adheres to the programming principle “Don’t Repeat Yourself”, or “DRY”.

3. How to define methods

In Java, every method starts with a return type, followed by a method name, parentheses, or round brackets, and a set of curly brackets, indicating a code block. Inside the curly brackets, we write the instructions we want the method to execute.

4. void method

Some methods simply perform an action. For example, if we want to print a greeting, we can create a method called `sayHello()`. Since it only prints a message and doesn't send any result back, we define it using the return type `void`. `void` literally means nothing, so this means that the method returns nothing - or doesn't give us anything back.

5. Method that produces result

But often, we want a method to produce a result, something we can store, use later, or pass to another part of our program. For example, we might write a method that calculates the square of a number and returns that result. In those cases, we use a specific return type, like int or String instead of void, and include a return statement in the method to send the value back using the return keyword.

6. Naming convention

The method name follows lower camel case - meaning the first letter is lowercase, and any additional words start with a capital letter. This is a convention in Java that makes method names easy to read.

7. Case sensitivity

Java is case-sensitive, so method names must be written exactly as defined. If a method is called `getSquare()`, writing `getsquare()` with lowercase S won't work. Following proper naming conventions makes our code easier to read and understand.

8. Built-in vs. Custom methods

There is an important difference between calling a method we define ourselves and calling a built-in method. With a built-in method like `.toLowerCase()`, we call it on a specific object - such as a `String` - using dot notation. But when we call a method we've written ourselves, we just use its name followed by parentheses, like `sayHello()` or `getSquare()`. We'll learn more about creating methods for objects in the next course.

9. Using a custom method

Finally, we need to define our methods outside the main method and also include the keyword static - we’ll learn why that is in the next course!

10. Recap

Custom methods allow us to write reusable, well-organized code. Here is a recap of what you've learned so far!

11. Let's practice!

Now, let's practice building a simple greeting method and a method that returns the square of a number!