Java Method
java method is very useful. It can be used to break a complex program into small manageable pieces. A java method is simply a block of statements that has a name and can be executed by calling it from some other place in the program. A java void method simply executes a group of statements and then terminates. A java value-returning method returns a value to the statement that called it. So far we have created a method named main in every program we've written, and we've executed predefined methods from the java API, such as System.out.println, Math.pow and so on. In this section, we will learn how to create our own methods, and how to execute them. We will focus on the void method and value-returning methods.
Defining a java void method
To create a method we must write
its definition, which consists of two general parts: a header and a body. We
learned about both of these in previous chapters.
Here is a review of how a header and a body is like
Header
public static void main(String[] args)
{
Body
System.out.println("Hello World");
}
Now lets see an example how we are going to write a simple void method
public static void display()
{
System.out.println("Hello World From the Method");
}
* public: The keyword indicates that the method's is able to use by any Java program that knows about the method.
* static:
This keyword declares that the method is a static method, which means that it
can be called without creating an instance of the class
in which it's defined.
* void: The keyword void is our return type. Simply, it means the method doesn't return any value.
* display: It's the name of our method. display(), will be use whenever we want to call the method.
* (): The parentheses can hold parameters, we can pass value into the method and be able to uses it.
* Method Body: The rest is the method body, which consists of executable statements such as if, while, and for.
Now, lets see some examples on calling
methods....
Java void method examples
Click here to download SimpleDisplay.java
Notice, when we declare the method, it has to be in the same class. To call the method in the main, we simply type the name of the method.
Lets observe another void method example
Click here to download LoopMethod.java
Now lets see another unique example, a void method inside another void method
Click here to download DoubleMethod.java
Now, lets look at value java returning method
A value returning method is basically a method that returns a value and we are able to uses its value to do whatever we want. Here is an example
Click here to download MethodAdd.java
Note, here we have public static int Sum(), where int determines the type we are returning. Then we call the value as how we call the void method.
Lets observe a different return-value method.
Click here to download NameReturn.java
Java Methods That Take Parameters
A parameter, is a value that we can pass to a method. The method then can use the parameter as if it were a local variable initialized with the value of the variable passed to it by the calling method. For a method that accepts parameters, it must list the parameters in the method declaration. The parameters are listed in a parameter list that's inside the parentheses that follow the method name. For each parameter used by the method, we must declare its type inside the parentheses. If more parameters are needed, we must separate the parameters by a comma. All these talking must be confusing, although try and remember what we just read and follow through the examples below.
Click here to download MinMax.java
This is call scoping out parameters, which means a parameter can have the same name as local variable used in other methods without causing any conflict. Hence, the above program we have min and max declared as local variable, in addition we declared min and max in the parameter list.
Pass by value
Click here to download PValue.java
Here we have a number variable set to 3 and then pass to the method name changeNum. The method receives the variable as a parameter named i, and then sets the value i to 1. Then back in main, we are trying to print out the number after changeNum. Since changeNum only gets a copy of the number and not the number variable itself, the output of the program will still be the original variable which is 3.
That's for methods, for more examples look in Java examples in the home page.