Java Methods

Method is the name given to subprograms or procedures in Java. All methods are functions; they return data. Although if we define a method that returns a "void" type, it means that it doesn´t return anything. E.g.:void print()

Parameters

A method may receive zero, one or more parameters. The parameters are input data that can be inserted in the method call. Within the body of the method, the parameters can be used as variables.

In the definition of the parameters of a method, we indicate what type they are. When we have two or more parameters, they are separated by commas. 

E.g.: int add(int a, int b).

Method declaration

A method includes a header and a body. To declare a method header, simply write the return type, followed by the method name and in parentheses the list of parameters.

Examples:

The body of a method, is a sequence of instructions separated by commas, between dos keys; {}. The sequence may be empty {}.

    class Example {
        void print() { } // this method is valid, but does nothing 
    
        int add(int a, int b) { // this method receives two numbers and returns their sum 
           return a + b;
        }
    }

To call a method;

   Example example = new Example();
   example.print();
   int result1 = example.add(4,6); // we call the method add, with the argumnents 4 and 6
   int result2 = example.add(3,3); // we call the method add, with the argumnents 3 and 3 
  

Arguments

When we call a method with parameters, we have to give values to each of the parameters. These values are called arguments.

Member of an object

As seen in the example code above, you must create an object before accessing methods. These methods are associated with objects. Therefore, these methods are called members of an object.

Static methods

There is a second type of methods in Java, which are associated with a class, rather than to the objects created by the class.

To create a method of this type, simply add the static keyword before the return type of the method. With this, the method will be associated with the class.

A static method is analogous to static variables and it is normal to see them work together.

In the example below, we create a method to increase the counter of people, which we had created in the example of static variables:

 

 

    class Person{
       static int totalNumberPeople;
    
       static int addNewPerson(){
          Person.totalNumberPeople = Person.totalNumberPeople + 1;
       }
       ...
    }
  

 

To use the method:

    Person.addNewPerson();
    Person person = new Person();
  

Access control, public method.

By default, a method can only be accessed from the same class or package. In the future we will see more about package and access control.

For now, we are going to focus on the public modifier. In order to obtain a method with no access restrictions, we only have to write the word public before the declaration of the method.

Main method

There is a special method to start the execution of a java program. This method must be called main, be static and public. It has to receive as parameter an array of Strings, to receive the arguments sent at the start of the execution.

	public static void main(String[] args) {
	   
	}
 

At this point, we have learned everything we need to create our first executable Java program. But before we start, we have to install the development tools.