1

While runnnig the code this error is produced

Employee cannot be applied to given types;  
  required: no arguments  
  found: java.lang.String,double,int  
  reason: actual and formal argument lists differ in length

Main Class:

public class Salary {
    public static void main(String[] args) {
        Employee worker = new Employee("harry",5.5,45);
        System.out.println(worker.getEmployeeName());
        System.out.println("Your Gross Salary is"+ worker.getGrossSalary());
        System.out.println("after tax is"+ worker.getNetSalary());

    }

}

Constructor form the class Employee:

public void Employee(String n , double pr, int h){
    name = n;
    payRate = pr;
    hours = h; 
}
qfwfq
  • 2,416
  • 1
  • 17
  • 30
Phil Horwood
  • 11
  • 1
  • 2
  • `public void Employee` is not a constructor. You declare the return type only for methods. `public Employee` is a constructor. – BackSlash Feb 25 '17 at 23:09

1 Answers1

2

The constructor can't & should never have a return type.

public void Employee(String n, double pr, int h){ // this is considered a method that belongs to that particular object
    name = n;
    payRate = pr;
    hours = h; 
}

change to this:

public Employee(String n , double pr, int h){ // this is considered a constructor, invoked when you're making an object of this type
    name = n;
    payRate = pr;
    hours = h; 
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126