Methods: Introduction, what they are, what they are for, how and when to use methods

To have a section in our Progressive Java course, you should suspect the importance of methods.

In this article, we give a description of what methods are, what they are for, how to declare, how to use and when to use.



Introduction to Java methods


Until now, we create applications that always followed the pattern of the code sequence.
That is, from top to bottom.

O que são os métdos (methods) em Java, como declarar e usar.jpgWith the use of loops (while, for, do ... while), conditional tests if else and switch statements, break and continue, we now have a little more control and slightly altered the flow, ie the way that programs occurred.
But they still occur from the top down.

Methods are nothing more than a block of code that can be accessed anytime and anywhere in our programs.
That is, they dont need be located in the order of 'top-down' in the code.


The usefulness of the methods in Java

The usefulness are two:

1. Organization
Everything is possible with the methods, it is possible to do without.
However, Java programs would be huge, messy and worse, we would have to repeat the same piece of code over and over again.

One of the great advantages, if not the greatest, which we will show below, of using the methods in  Java is that we write a piece of code once (which are the sayings whose methods) and can access them several times.

2. Reusability
When you start a big project, you hardly have to do everything from scratch.
If you do, it's probably because you are very badly organized or did not studied with Progressive Java Course.

Here's one of the most important tips of Java Progressive course:
Create methods that do specific things and well defined.
This is useful for reusability.

What we mean by specific methods?

Instead of creating a method that builds a house, create a method that creates a bedroom, another method that creates the room other than the creates bathroom, a method to organize the house etc..
O que são os métdos (methods) em Java, como declarar e usar.jpgSo when you create a bathroom that already have the specific method for that.
See methods like pieces of a puzzle. But parts are 'Joker', which fit with many other pieces.


Reusability is time. And time is money.
For example, in the study of this course, you will create a method that returns the roots of a quadratic equation.
Keep this method with a name you remember.
In future work, you need this method. And there's going to always type all over again?

Of course not! Get organized! Remember that this method has already done, you saved, go there, copy and put on your new project.


How to declare methods in Java

There are many, many ways to declare a method. Some details only we will explain better when you know Object Orientation.

The syntax 'general' methods is as follows:


[characteristcs of the method] method_name(parameters types) {
    // code 
    // of yours
    // method
}



Using / calling methods


Let's create a method that simply prints the message on the screen: "Progressive Java Course"
Let's call this method 'message'.

He will be a public method (public) and will not return anything (void).
A method that computes the delta of the quadratic equation, returns the value of delta (a float).
In our case, the method will just print a message on the screen.
The 'static' as well as 'public' and 'void' will be explained throughout the course.
It will look like this:


    public static void message(){
        System.out.println("Progressive Java Course!");
    }



We put this method in some class. In our case, within the class that has the method 'main'.

Ready. Code written, method declared. And about how using the method?
Calling! What do you call something? By name!
Just write 'message ();' in your program where you want, that method will be called and executed.

Test:


public class testMethod {

    public static void message(){
        System.out.println("Progressive Java Course!");
    }
    
    public static void main(String[] args) {
        System.out.print("Dispĺaying the message once: ");
        message();
        
        System.out.println("Displaying 3 times:");
        for(int count=1 ; count<=3 ; count++){
            message();
        }
    }
}



Note that our class now has two methods.
One method is the 'main', which serves to start Java applications. The other display a message.

Already here we note one important detail: we can, and often invoke a method inside another.
We were in the 'main ()', when we invoke the 'message ();'.

Note also another detail: instead of writing several times:
System.out.println ("Progressive Java Course!");

We write only once, in the method, and the method call. More practical, isn't?
This Java!

We will show in the next example, how to create an interactive menu using the methods.
You will see that with the methods things get more organized, it saves lines of code and is easier to understand when looking at the codes. Beyond the question of reusability of code, since you can always use these methods in other applications.

No comments: