Methods
Java has two kind of methods, a method that return or compute a value and a method that does not return a value. For example, the method println of the object System.out is an example of a method that performs an action other than returning a value. In this case, the action is to write something to the screen.
The method nextInt of the class Scanner, introduced early on, is a method that returns a value. In this case, the value returned is a number typed in by the user.
A method that performs some action other than returning a value is called a void method. This same distinction between void methods and methods that return a value applies to methods in the classes you define. The two kinds of methods require slight differences in how they are defined.Static methods
In Java, you can define a method that does not require a calling object. Such methods are known as static methods. You define a static method in the same way as any other method, but you add the keyword static to the method definition heading.static returning type method
The definition of a method that returns a value must have one or more return statements. A return statement specifies the value returned by the method and ends the method invocation.
Syntax
static + data type + method_name (arguments)
{
statements;
}
Below is a program that computes the sum of two integers a and b
static int sum(int a, int b)
{
int c = a+b;
return c;
}
To invoke the method in the main program, just call the method name, pass the required arguments into it and store it in the variable with the same data type of the return value.
int z= sum(5,7);
System.out.print("the sum is: "+z);
this displays the sum is 12.Non returning type method
The definition of a method that does not return a value has a reserved key word void followed by the method name and arguments if nedeed.
Syntax
The syntax of a non returning type method is:
static + void + method_name(arguments)
static void displayText()
{
System.out.println("I like programming");
}