Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating Our Own Methods | Methods
Java Extended

Creating Our Own MethodsCreating Our Own Methods

How to use methods?

In the previous chapter, we looked at creating a method that adds two numbers. Now let's see how we can actually call this method in our code:

java

Main.java

Note

You may have noticed that we added static before the method signature. For now, you don't need to pay attention to this keyword. We are using it only because the main method is static by default, and static methods can only use other static methods within them.

  • We created two variables of type int with different values and then passed them as parameters to our method;
  • Since our method returns an int type, we can initialize the variable sum with the result of calling the method;
  • Thus, our method calculated and returned the sum of the two numbers.

We can also pass arrays as parameters and return them from methods. Let's look at an example:

java

Main.java

We have written a method that sorts and increments each element of an integer array (int[]). Then we create an unsorted array and use the method on it, initializing a new array called newArray with the returned value.

It is worth noting that we can use this method multiple times in the same code, for example:

java

Main.java

Thus, we can see that we can use our method multiple times in the code. We have used it on two different integer arrays, and each of them is now sorted and incremented.

Everything was clear?

Section 2. Chapter 2

Creating Our Own MethodsCreating Our Own Methods

How to use methods?

In the previous chapter, we looked at creating a method that adds two numbers. Now let's see how we can actually call this method in our code:

java

Main.java

Note

You may have noticed that we added static before the method signature. For now, you don't need to pay attention to this keyword. We are using it only because the main method is static by default, and static methods can only use other static methods within them.

  • We created two variables of type int with different values and then passed them as parameters to our method;
  • Since our method returns an int type, we can initialize the variable sum with the result of calling the method;
  • Thus, our method calculated and returned the sum of the two numbers.

We can also pass arrays as parameters and return them from methods. Let's look at an example:

java

Main.java

We have written a method that sorts and increments each element of an integer array (int[]). Then we create an unsorted array and use the method on it, initializing a new array called newArray with the returned value.

It is worth noting that we can use this method multiple times in the same code, for example:

java

Main.java

Thus, we can see that we can use our method multiple times in the code. We have used it on two different integer arrays, and each of them is now sorted and incremented.

Everything was clear?

Section 2. Chapter 2
some-alt