You are missing missing "static" keyword in main method public static void main(String[] args)
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
wendellrob,
if you make all your variables and the computeNetPay() method static you'll be able to access them from main() (that should be also static). Since your variables are static you don't have to create an instance of your class (Pay).
If you don't want to declare your variables and computeNetPay() method static and still make a call to computeNetPay() from main() then you have to create in main() an instance of your class Pay. Then you can access these variables and method through the instance.
Of course you can also do what quuba mentioned: create an instance of your Pay class in main() method and call computeNetPay() method within the constructor.
freelancelote
Junior Poster in Training
89 posts since Aug 2008
Reputation Points: 34
Solved Threads: 2
The class that contians 'public static void main' must have static declared for all methods contained.
That is not true.
Static is used so that a class can call methods on itself without having to create a specific object of that class. Static methods in a class can be called without having to instantiate an object of that type. If you changed your computeNetPay() method to static then you could call it in the main() method. However the OO way is to use the main method to create an instance of that class, in the form of an object, and then use it's constructor to call methods (which is what freelancelote and quuba said). An example using your code would be.
public class Pay{
private double hoursWorked = 40;
private double rateofPayPerHour = 10;
private double withholdingRate = .15;
private double grossPay;
private double netPay;
//added static modifier
public static void main(String[] args){
//this instantiates an object of the type Pay, which calls its constructor
new Pay();
}
//Pay's constructor, a special type of method that creates object
public Pay(){
//now this is where you can run your method
computeNetPay(hoursWorked, rateofPayPerHour, withholdingRate);
}
public void computeNetPay(double hoursWorked, double rateofPayPerHour, double withholdingRate){
double withHolding;
grossPay = hoursWorked * rateofPayPerHour;
withHolding = grossPay * withholdingRate;
netPay = grossPay - withHolding;
System.out.print(netPay);
}
}
jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53