Error Message Name:
Error: cannot find symbol
symbol : constructor {constructor name}{parameter list}
Example 1: Suppose we have the Test class:
private int num = 10;
public void Test(int num)
{ this.num = num;
}
public static void main(String[] args)
{ Test test = new Test(5);
}
Compiling the code will give us the following error message:
Error: cannot find symbol symbol : constructor Test(int)
Example 2: Suppose we have the Test class:
private int num = 10;
public Test(int num)
{ this.num = num;
}
public static void main(String[] args)
{ Test test = new Test(5.2);
}
This would output the following error message:
Error: cannot find symbol symbol : constructor test(double)
Cause/Solution: There are a couple of reasons as to why this is happening:
1) When you are coding the constructor, you included the keyword void in the signature. This is the case in Example 1. Remember, the constructor has no return type. To fix the problem, simply erase the keyword void from the method signature:
public Test(int num)
{ this.num = num;
}
2) When calling the constructor in the main method, you are using the wrong type of arguments. This is the case in Example 2. Similar to calling methods, you must make sure the parameter list match the argument list. In Example 2, the main method is using a type double as an argument. However, in the method signature, the parameter is type int. We can change the signature to public Test(double num), so that it accepts a double as an argument. Another solution would be to the change the argument to type int, so that it matches the parameter:
public static void main(String[] args)
{ Test test = new Test(5);
}