What is "Actual and formal argument lists differ in length" #
First, let's understand what is actual argument and formal argument.
- Formal argument: The arguments we define for a method or constructor.
- Actual argument: The arguments we pass into a method or constructor.
Let's see an example:
public static void main(String[] args) {
int num_1 = 10;
int num_2 = 20;
int sum = addition(num_1, num_2);
}
public static int addition(int a, int b){
return a + b;
}
From the above example, a and b is the formal arguments while num_1 and num_2 is the actual arguments that we pass into the method addition.
Now, we've understood the difference between actual argument and formal argument. Let's see another example:
public static void main(String[] args) {
int num_1 = 10;
int num_2 = 20;
int sum = addition(num_1);
}
public static int addition(int a, int b){
return a + b;
}
The compilation error occurs:
method addition in class NewMain cannot be applied to given types;
required: int,int
found: int
reason: actual and formal argument lists differ in length
Here, we pass in only one actual argument, num_1 but the method, addition actually defines two formal arguments, a and b to work. Let's see another example:
public static void main(String[] args) {
int num_1 = 10;
int num_2 = 20;
int num_3 = 30;
int sum = addition(num_1, num_2, num_3);
}
public static int addition(int a, int b){
return a + b;
}
The compilation error also occurs:
method addition in class NewMain cannot be applied to given types;
required: int,int
found: int,int,int
reason: actual and formal argument lists differ in length
Here, we pass in three actual arguments, num_1, num_2, and num_3 while the method, addition only need two formal arguments to work.
So far we've discovered this problem in the method invocation. Let's see some examples of constructor:
public class Student {
private String first_Name;
private String last_Name;
public Student(String first_Name, String last_Name){
this.first_Name = first_Name;
this.last_Name = last_Name;
}
}
Here, we declare a class, Student, where we need first_Name and last_Name arguments in the constructor to create an instance of Student.
Student student = new Student("Alex");
Here is the compilation error:
constructor Student in class Student cannot be applied to given types;
required: String,String
found: String
reason: actual and formal argument lists differ in length
Let's move on to the next section to see why it occurs.
Solution #
You may guess the solution. Yes, you just need to remember the main concept while defining and using the methods / constructors:
The actual and formal argument lists should always be same in length
What means? Before using the methods or constructors, you should check the number of formal arguments defined for a method or constructor. When you want to invoke a method or initiate an instance of object using constructor, make sure the number of actual arguments passed in is same as the number of formal arguments.
public static void main(String[] args) {
int num_1 = 10;
int num_2 = 20;
int num_3 = 30;
int sum = addition(num_1, num_2, num_3);
System.out.println(sum);
}
public static int addition(int a, int b, int c){
return a + b + c;
}
This is the output:
60
We can either increase / decrease the number of formal arguments defined or the number of actual arguments passed in. Just to make sure both are having the same length.
For a Class, if you didn't override its constructor, the constructor has no any formal arguments
public class Student {
private String first_Name;
private String last_Name;
public String getFirst_Name() {
return first_Name;
}
public void setFirst_Name(String first_Name) {
this.first_Name = first_Name;
}
public String getLast_Name() {
return last_Name;
}
public void setLast_Name(String last_Name) {
this.last_Name = last_Name;
}
}
I define a class, Student without overriding its default constructor.
Student student = new Student();
The compilation error will not occur and we can assign value to the variable, student using setter method in the class.