0

Like most new programmers I have a small but significant issue that i cannot figure out. My program will not pull my constructor. I have tried quite a few different ways and cant seem to figure it out. Any help would be greatly appreciated.

Error
EmployeeTest.java:13: cannot find symbol
symbol  : constructor Employee()
location: class Employee
    Employee x = new Employee();
                 ^
EmployeeTest.java:14: cannot find symbol
symbol  : constructor Employee()
location: class Employee
    Employee y = new Employee();
public class Employee
{
private double salaryValue; // variable that stores monthlySalary
private String firstName; // instance variable that stores first name
private String lastName; // variable that stores last name

 public Employee( String firstNameParameter , String lastNameParameter ,  double          salaryValueParameter )
{

    if ( salaryValueParameter < 0.0 ) // validate monthlySalary > 0.0
    salaryValue = 0.0; // if not salary is intitalized to default

    else 

      firstName = firstNameParameter;
      lastName = lastNameParameter;
      salaryValue = salaryValueParameter;
}  

 public class EmployeeTest 
{
public static void main( String[] args )
{   
String temp;
Double temp2;
Double temp3;

Employee x = new Employee();
Employee y = new Employee();
1
  • 1
    By the way, double is often not the best data type to model currency quantities due to the rounding errors that can accumulate over time. Commented Oct 4, 2010 at 19:51

8 Answers 8

10

Because you've added a constructor that takes 3 arguments, the Employee class no longer has a default constructor - one which takes no arguments. So you can't do this:

Employee x = new Employee();

and you must include 3 arguments:

Employee x = new Employee("firstname", "lastname", 123.45);

If you want to instantiate an Employee without supplying any parameters, you must add a no-argument constructor:

public Employee() {
}

You can read more about default constructors in section 8.8.9 of the Java Language Specification.

Sign up to request clarification or add additional context in comments.

Comments

3

You're calling a constructor without any parameters:

new Employee()

This causes Java to look for this default constructor in your class:

public Employee() {}

Which it can't find because you have a custom constructor with parameters, hence the error. Your Employee class only has this constructor:

public Employee(String, String, double) {}

You should either pass parameters to the constructors in your new statements, or declare that default parameter-less constructor explicitly and implement it (as an overload of your other constructor, perhaps to pass in default values or something).

Comments

2

By default, a class has a no-arg constructor. I.e.

 public Employee()

If you later and go add your own constructor.. i.e.

 public Employee( String name )

Then the listed ones are the only ones you can use.

If you still want to call the default one, add it back in.

public Employee()

Comments

0

You have to add a default public constructor:

public void Employee();

Comments

0

You have to define your default constructor. When you implement a custom one, the default is lost

public class Employee{
   public Employee(){}
...
}

Here is a short article on constructors

Comments

0

Your constructor defines String firstNameParameter , String lastNameParameter , double salaryValueParameter

as parameters. When you call Employee x = new Employee(); you are calling a non-existant constructor. Try the following instead:

Employee x = new Employee('David', 'Jones', 50000);

Comments

0

You are attempting to create a new class with an empty constructor, but the only constructor you have declared is one requiring parameters. You should probably try something like:

Employee x = new Employee("Bob", "Jones", "100.0");

Or, declare a default constructor:

public Employee() {
    //do cool stuff here
}

Hope that helps.

Comments

0

You provide only a constructor with three arguments, but you try to call a constructor without arguments. Such a constructur is included automatically by the compiler but only if your class has no other constructors.

So you have two options:

  • Include another constructor without arguments
  • call the constructor with the right number and types of arguments

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.