In this article, we will talk about the method parameter and method argument. Please make a note that the method argument and method parameter are sometimes used interchangeably. Parameters refer to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration’s parameters in type and order.
public class Method_Parameter {
public static void main(String[] args) {
String name = "javabeginnerstutorial.com";
int age = 10;
JBT instance = new JBT();
instance.method(name, age);
}
}
class JBT {
void method(String parameter1, int parameter2) {
System.out.println("1st Parameter :" + parameter1 + " , 2nd Parameter : " + parameter2);
}
}
A method declared in the above code has two parameters, parameter1 & parameter2 of type String and int respectively. The same method is invoked at line no 8, passing the same type of argument named name and age, in given sequence only. As you can see name can be different but type of argument and sequence should be the same as the parameter defined in the method declaration.
Parameter name
Name given to the method parameter is used within the method body to refer to the passed-in argument. Though there is no restriction on the name of a method parameter, still it has to follow some rules.
- It must be unique in its scope.
- It cannot be the same as a local variable in the given method.
It is fine to have the same name parameter as one of the class’s fields. In this case, parameter is said to shadow the field. But it is not a recommended way coding as it will make your code difficult to understand. It should be used only for setter methods, where we set a specific field. When you want to set specific field of the same name from class, you should use this keyword in java.
class JBT {
String parameter1 = "VNG";
void method(String parameter1, int parameter2) {
System.out.println("1st Parameter :" + parameter1 + " , 2nd Parameter : " + parameter2);
System.out.println("Class level parameter1 value :"+ this.parameter1);
}
}
Pass by reference or value
Java is always pass-by-value(value of reference). It means that when you call a method, a copy of each argument is passed. You can do whatever you want with that copy inside the method but that will not impact the actual parameter. Though it is always pass-by-value some times it looks like it is passed by reference especially when you pass some object.
Passing primitive types
There are 8 primitive data types in Java. When any variable of primitive types, passed to a method as parameters, their original values will not change.
public class Method_Parameter {
public static void main(String[] args) {
String name = "JBT";
int age = 10;
System.out.println("Value :: 1st Parameter -" + name + " , 2nd Parameter -" + age);
JBT instance = new JBT();
instance.method(name, age);
System.out.println("Value :: 1st Parameter -" + name + " , 2nd Parameter -" + age);
}
}
class JBT {
void method(String name, int age) {
name = name.concat(".com");
age++;
System.out.println("Value in method:: 1st Parameter -" + name + " , 2nd Parameter -" + age);
}
}
Passing Object
Java Object is also pass-by-value. Still, the value which is passed is actually the reference of the object hence when any variable of object types, passed to a method as parameters, their original values will get changed. As value/variable which is being referred is the actual object’s variable.
public class Method_Parameter {
public static void main(String[] args) {
JBT instance = new JBT();
System.out.println("Value in Name :" + instance.getName() + " , Value in Age :" + instance.getAge());
instance.method(instance);
System.out.println("Value in Name :" + instance.getName() + " , Value in Age :" + instance.getAge());
}
}
class JBT {
String name;
int age;
void method(JBT jbt) {
jbt.setAge(30);
jbt.setName("VNG");
System.out.println("Values changed inside method.");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Here instance variable @line 4 is a reference to the object created on the right side of =. When we pass the value of this reference in any method, method parameter holds the value of reference, so technically it is also referring the same object as method argument. As a result, changes are done in any part of the Object will get reflected in the actual object.
Passing String or Wrapper types
Though String and Wrapper types (Integer / Boolean…..) are an object. Still, they behave a bit differently because String and Integer are immutable. So when we do changes inside methods it is actually creating the new object instead of modifying the Original Object being passed to the method.
public class Method_Parameter {
public static void main(String[] args) {
String name = "JBT";
Integer age = Integer.valueOf(10);
System.out.println("Value :: 1st Parameter -" + name + " , 2nd Parameter -" + age);
JBT instance = new JBT();
instance.method(name, age);
System.out.println("Value :: 1st Parameter -" + name + " , 2nd Parameter -" + age);
}
}
class JBT {
String name = "VNG";
int age = 20;
void method(String name, Integer age) {
name = name.concat(".com");
age = Integer.valueOf(12);
System.out.println("Value in method:: 1st Parameter -" + name + " , 2nd Parameter -" + age);
}
}