I have two classes. The first class is called EmployeePayRoll.java (this compiles fine) and the second is called TestEmployeePayRoll.java (this does not compile). The trouble I'm having is that I'm trying to pass in methods as parameters when I instantiate my objects in the main method of the test class. I've tried a bunch of different things and I just get errors. I've had static/non-static related errors, null pointer errors, etc... I've got some tweaks I'm going to make when I get the test class to compile but this is holding me up. Any advice or suggestions would be appreciated. Thanks!

public class EmployeePayRoll
{
   /*** Class Constants ***/

      /*** Defaults ***/

   public static final String DEFAULT_EMPLOYEE_NAME = "Employee";
   public static final double DEFAULT_PAY_RATE = 10.00;
   public static final double DEFAULT_HOURS_WORKED = 40.0;

      /*** Minimums/Maximums ***/

   public static final double MINIMUM_PAY_RATE = 7.25;
   public static final double MAXIMUM_PAY_RATE = 20.00;

   public static final double MINIMUM_HOURS_WORKED = 0.0;
   public static final double MAXIMUM_HOURS_WORKED = 168.0;

   /*** Instance Variables***/

   public String employeeLastName;
   public double hoursWorked;
   public double payRate;

   /*** Constructors ***/

   public EmployeePayRoll( String employeeName,double hours )
   {
      employeeLastName = employeeName;
      hoursWorked = setHoursWorked( hours );
      payRate = DEFAULT_PAY_RATE;
   }

   public EmployeePayRoll( String employeeName, double hours, double hourlyRate )
   {
      employeeLastName = employeeName;
      hoursWorked = setHoursWorked( hours );
      payRate = setPayRate( hourlyRate );
   }

   /*** Mutators ***/

   public double setHoursWorked( double hours )
   {
      if ( verifyHoursWorked ( hours ) )
      	 hoursWorked = hours;

      else
      	 hoursWorked = DEFAULT_HOURS_WORKED;

      return hoursWorked;
   }

   public double setPayRate( double hourlyRate )
   {
   	  if ( verifyPayRate ( hourlyRate ) )
   	     payRate = hourlyRate;

   	  else
   	  	 payRate = DEFAULT_PAY_RATE;

   	  return payRate;
   }

   /*** Accessors ***/

   public double getHoursWorked()
   {
   	  return hoursWorked;
   }

   public double getPayRate()
   {
   	  return payRate;
   }

   public double calculateGrossPay()
   {
   	  return hoursWorked * payRate;
   }

   /*** Displays information used to calculate pay ***/

   public void displayEmployeeData()
   {
      System.out.println( "\n\n" );
      System.out.println( "         Employee: " + employeeLastName    + "\n" );
      System.out.println( "  Hourly Pay Rate: " + "$" + payRate       + "\n" );
      System.out.println( "     Hours Worked: " + hoursWorked         + "\n" );
      System.out.println( "        Gross Pay: " + calculateGrossPay() + "\n" );
   }

   /*** Helper Methods ***/

   public boolean verifyPayRate( double hourlyRate )
   {
      boolean payRateVerification = false;

	  if ( hourlyRate >= MINIMUM_PAY_RATE )
	  {
	     if ( hourlyRate <= MAXIMUM_PAY_RATE )
		 {
		    payRateVerification = true;
		 }
	  }
	  return payRateVerification;
   }

   public boolean verifyHoursWorked( double hours )
   {
      boolean hoursVerification = false;

	  if ( hours >= MINIMUM_HOURS_WORKED )
	  {
	     if ( hours <= MAXIMUM_HOURS_WORKED )
		 {
		    hoursVerification = true;
		 }
	  }
	  return hoursVerification;
   }
}
import java.util.Scanner;

public class TestEmployeePayRoll
{
   /*** Instantiates scanner ***/

   Scanner scanner = new Scanner( System.in );

   /*** Gets input from user ***/

   public String inputName()
   {
   	  System.out.println( "\n Last name: " );
   	  EmployeePayRoll.employeeLastName = scanner.next();
   	  return EmployeePayRoll.employeeLastName;
   }

   public double inputPayRate()
   {
   	  System.out.println( "\n  Pay rate: " );
   	  double rate = scanner.nextDouble();
   	  return EmployeePayRoll.setPayRate( rate );
   }

   public double inputHoursWorked()
   {
   	  System.out.println( "\n     Hours: " );
   	  double hrs = scanner.nextDouble();
   	  return EmployeePayRoll.setHoursWorked( hrs );
   }
  
   /*** Main method ***/

   public static void main( String argv[] )
   {
   	  TestEmployeePayRoll test = new TestEmployeePayRoll();

   	  /*** Instantiates employees ***/

   	  EmployeePayRoll employee1 = new EmployeePayRoll( test.inputName(),
   	                                                   test.inputHoursWorked() );

   	  EmployeePayRoll employee2 = new EmployeePayRoll( test.inputName(),
   	                                                   test.inputHoursWorked(),
   	                                                   test.inputPayRate() );
   }
}

By the way, I know payroll is one word, but I have to follow from what my professor gave me in the specs.

Dani AI

Generated

Useful follow-up and quick checklist based on what and discussed.

The root cause was treating instance state and behavior as if they were static. Instance fields and non-static methods (like your employeeLastName, setPayRate(...), setHoursWorked(...)) must be accessed on an object, not on the class. A default constructor fixes the immediate compile error because it lets you create an object before calling instance methods, but a cleaner approach is to collect input first and then construct the object with the right constructor or create the object and then call its setters.

A practical, minimal workflow:

  • Read input into local variables.
  • Validate or catch input errors (InputMismatchException).
  • Construct the EmployeePayRoll instance with those values, or construct a default instance and call instance setters.
  • Close the Scanner when done.

Example pattern:

Scanner sc = new Scanner(System.in);
String name = sc.next();
double hours = sc.nextDouble();
double rate = sc.nextDouble();
EmployeePayRoll emp = new EmployeePayRoll(name, hours, rate);
emp.displayEmployeeData();
sc.close();

Design notes and best practices you can apply now:

  • Make fields private and expose controlled getters/setters to protect invariants.
  • Prefer setters that return void; keep validation separate so the API is clear.
  • For production payroll use BigDecimal to avoid floating-point rounding issues (double is fine for learning exercises).
  • Handle bad input with try/catch and re-prompt rather than letting the program crash.

Quick troubleshooting if you see the same errors again:

  • "cannot make a static reference to the non-static method" => call method on an instance.
  • NullPointerException => you tried to use an object reference that is null (instantiate it first).
  • InputMismatchException => validate scanner input or use hasNextDouble()/hasNextInt().

This keeps the code safer and clearer than relying on static access or ad-hoc default constructors.

Recommended Answers

All 5 Replies

Ok, the first thing I see is that you're trying to make a static reference to a non-static method in EmployeePayRoll - that's not allowed. You wouldn't want to do this anyway, since the value would never be saved. In your test class, instantiate an object of type EmployeePayRoll, e.g., empPayRoll, and use that instead. To do this, you'll either need a default constructor in EmployeePayRoll (since you don't have the values to populate that class yet), or you'll need to collect the name, hours and rate in separate variables in each of your input methods and construct your instance of EmployeePayRoll from that. That should give you a start.

I don't understand what you mean by

and use that instead

Instead of what?

For example:

EmployeePayRoll empPayRoll = new EmployeePayRoll();

Use the

empPayRoll

instance instead of the

EmployeePayRoll

class in your methods where you set the values of the object.
Again, this requires you to create a default constructor for the

EmployeePayRoll

class.

Ok I think I see what you're saying now. I had tried that before but I didn't have a default constructor. I'll try again with another constructor.

It worked. Thank you very much for your help! I've been trying to get this figured out on and off for days. I knew it would be something pretty simple. It's too bad that I had to come to DaniWeb to figure out what my professor should have been able to tell me days ago. Thank you again! I really appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.