JAVA Methods
Table of Contents
JAVA Methods
JAVA Methods
A function defined in another programming language is analogous to a Java methods. A method explains how an object behaves. A method is made up of several statements that work together to complete an action.
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 JAVA Methods.
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 JAVA Methods:
Lets understand the method by simple example that takes a parameter and returns a string value.
public String getName(String st)
{
String name="onlineexamguide";
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
To carry out the functionality built into it, methods are called. By using the method’s name, we can invoke it and assign the result to a variable.
String val = GetName(".com")
It will return a value onlineexamguide.com after appending the argument passed during method call.
Returning Multiple values
Using an array in Java, we may have a method return many results. We put all the values we want to return into an array, which we then send back to the calling method. When creating an array, we must specify the return-type as an array. Here is an illustration.
Example:
So 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 JAVA Methods
In some circumstances, it could be necessary to return a class object to the calling function. So In this scenario, the method definition must include the class name. The example that follows shows how to obtain an object from a method call. It can also be used to retrieve data collecting.
Example:
So 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
It’s crucial to understand the distinction between the terms parameter and argument while discussing methods. A method-defined variable called a parameter receives a value when the method is called. Parameters never have a scope outside of the method; they are always local to the method. When a method is called, a value is supplied to it as the while argument.
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 argument value.
NOTE :However there is no concept of call-by-reference in Java. Java supports only call by value.
Example of call-by-value
So 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
So See, in the above example, value passed to the method does not change even after modified in the method. So It shows that changes made to the value was local and argument was passed as call-by-value.