Michael
Michael

Reputation: 1

Java cannot find constructor

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();

Upvotes: 0

Views: 10689

Answers (8)

Richard Fearn
Richard Fearn

Reputation: 25481

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.

Upvotes: 10

Landei
Landei

Reputation: 54574

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

Upvotes: 0

aperkins
aperkins

Reputation: 13114

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.

Upvotes: 0

bwawok
bwawok

Reputation: 15347

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()

Upvotes: 2

DJ Quimby
DJ Quimby

Reputation: 3699

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);

Upvotes: 0

Tom
Tom

Reputation: 45104

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

Upvotes: 0

Boris Pavlović
Boris Pavlović

Reputation: 64622

You have to add a default public constructor:

public void Employee();

Upvotes: 0

BoltClock
BoltClock

Reputation: 723398

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).

Upvotes: 3

Related Questions