Methods in Java
Method in Java is similar to a function defined in other programming languages. Method describes behavior of an object. A method is a collection of statements that are grouped together to perform an operation.
For example, if we have a class Human, then this class should have methods like eating(), walking(), talking() etc, which describes the behavior of the object.
Declaring method is similar to function. See the syntax to declare the method in Java.
return-type methodName(parameter-list)
{
//body of method
}
return-type refers to the type of value returned by the method.
methodName is a valid meaningful name that represent name of a method.
parameter-list represents list of parameters accepted by this method.
Method may have an optional return statement that is used to return value to the caller function.
Example of a Method:
Lets understand the method by simple example that takes a parameter and returns a string value.
public String getName(String st)
{
String name="StudyTonight";
name=name+st;
return name;
}
Modifier : Modifier are access type of method. We will discuss it in detail later.
Return Type : A method may return value. Data type of value return by a method is declare in method heading.
Method name : Actual name of the method.
Parameter : Value passed to a method.
Method body : collection of statement that defines what method does.
Calling a Method
Methods are called to perform the functionality implemented in it. We can call method by its name and store the returned value into a variable.
String val = GetName(".com")
It will return a value studytonight.com after appending the argument passed during method call.
Returning Multiple values
In Java, we can return multiple values from a method by using array. We store all the values into an array that want to return and then return back it to the caller method. We must specify return-type as an array while creating an array. Lets see an example.
Example:
Below is an example in which we return an array that holds multiple values.
class MethodDemo2{
static int[] total(int a, int b)
{
int[] s = new int[2];
s[0] = a + b;
s[1] = a - b;
return s;
}
public static void main(String[] args)
{
int[] s = total(200, 70);
System.out.println("Addition = " + s[0]);
System.out.println("Subtraction = " + s[1]);
}
}
Return Object from Method
In some scenario there can be need to return object of a class to the caller function. In this case, we must specify class name in the method definition.
Below is an example in which we are getting an object from the method call. It can also be used to return collection of data.
Example:
In this example, we created a method get() that returns object of Demo class.
class Demo{
int a;
double b;
int c;
Demo(int m, double d, int a)
{
a = m;
b = d;
c = a;
}
}
class MethodDemo4{
static Demo get(int x, int y)
{
return new Demo(x * y, (double)x / y, (x + y));
}
public static void main(String[] args)
{
Demo ans = get(25, 5);
System.out.println("Multiplication = " + ans.a);
System.out.println("Division = " + ans.b);
System.out.println("Addition = " + ans.c);
}
}
Parameter Vs. Argument in a Method
While talking about method, it is important to know the difference between two terms parameter and argument.
Parameter is variable defined by a method that receives value when the method is called. Parameter are always local to the method they dont have scope outside the method. While argument is a value that is passed to a method when it is called.
You can understand it by the below image that explain parameter and argument using a program example.
call-by-value
and call-by-reference
There are two ways to pass an argument to a method
- call-by-value : In this approach copy of an argument value is pass to a method. Changes made to the argument value inside the method will have no effect on the arguments.
- call-by-reference : In this reference of an argument is pass to a method. Any changes made inside the method will affect the agrument value.
NOTE :However there is no concept of call-by-reference in Java. Java supports only call by value.
Example of call-by-value
Lets see an example in which we are passing argument to a method and modifying its value.
public class Test
{
public void callByValue(int x)
{
x=100;
}
public static void main(String[] args)
{
int x=50;
Test t = new Test();
t.callByValue(x); //function call
System.out.println(x);
}
}
50
See, in the above example, value passed to the method does not change even after modified in the method. It shows that changes made to the value was local and argument was passed as call-by-value.