Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8829227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:47:14+00:00 2026-06-14T07:47:14+00:00

So I’m new to java and I’m trying to wrap my head around this.

  • 0

So I’m new to java and I’m trying to wrap my head around this. So far I’m writing a public method that calls a private method, both of which are written in the same class. When testing the public method, I am able to call the public method against an object, employeeOne, whose parameters are supplied by the user. I’m not exactly sure whats going on with the private method call here though since it appears that I’m calling it on the class (i think) and not the objects with defined attributes.

So here is a private method that I have written inside a class called Employee:

private static double computeGrossPay()
{
    if (hoursWorked <= 40)
    {
        grossPay = (hoursWorked * payRate);
    }
    else if (hoursWorked >= 40)
    {
        grossPay = ((40 * payRate) + ((1.5 * payRate) * (hoursWorked - 40)));
    }
    return grossPay;
}

I was trying to figure out a way to call this method in another class and obviously since it is a private method I can’t call it anywhere outside of the class it is written in. So I wrote a public method that makes a call to the private method

public double grossPayDisplay()
{
    return Employee.computeGrossPay();
}

This is where my question comes into play: So far this code works but I’m not entirely clear on why it works.

Here is how I tested it:

System.out.println(employeeOne.grossPayDisplay());

employeeOne is an object created from the class Employee whose parameters are supplied by the user, my question is:

How does the compiler go from Employee in Employee.computeGrossPay() to employeeOne?

Or to reiterate how does

return Employee.computeGrossPay() 

actually pass attributes to computeGrossPay()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:47:16+00:00Added an answer on June 14, 2026 at 7:47 am

    computeGrossPay is a static method, which means it belongs to the class itself, rather an any particular Employee object. Static methods can only access other static members, so it looks like your fields like hoursWorked must be static too.

    This “works” but it doesn’t seem correct to me. A field like hoursWorked is an attribute that should belong to each individual Employee – try removing static from that field’s declaration.

    Now, you’ll probably get a compile error, since computeGrossPay is trying to access an instance (non-static) field when that method isn’t being called on an instance of Employee. For this reason computeGrossPay should probably be an instance method:

    private double computeGrossPay() { ... }
    

    And then it would be called like this:

    public double grossPayDisplay()
    {
        return this.computeGrossPay();
    }
    

    (which makes grossPayDisplay look a little pointless – you could just make computeGrossPay public)

    Alternatively, computeGrossPay could stay static and take an Employee as an argument:

    private static double computeGrossPay(Employee employee)
    {
        if (employee.hoursWorked <= 40)
        {
            employee.grossPay = (employee.hoursWorked * payRate);
        } else if (hoursWorked >= 40)
        {
            employee.grossPay = ((40 * payRate) + ((1.5 * payRate) * (employee.hoursWorked - 40)));
        }
        return employee.grossPay;
    }
    

    But that’s pretty ugly and doesn’t make as much sense.

    Above, I just assumed grossPay was also made an instance field too – but it feels a little strange how it’s being used. Since gross pay is something being calculated on the fly, it doesn’t seem like that should be saved in a field. Consider making it a local variable within the method:

    private double computeGrossPay()
    {
        double grossPay; //declare the local variable
    
        //assign it depending on hoursWorked
        if (hoursWorked <= 40)
        {
            grossPay = (hoursWorked * payRate);
        }
        else
        {
            grossPay = ((40 * payRate) + ((1.5 * payRate) * (hoursWorked - 40)));
        }
    
        //return its value
        return grossPay;
    }
    

    (doesn’t matter whether the method is static or not for that last point)

    One last note: you’ll notice I kept treating payRate like a static field. I just did that as an example, but it seems like it could go either way: there could be a single pay rate for all employees, or each employee could have their own pay rate. That just depends on the context of your program and it’s up to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.