• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to call a method from another class with multiple arguments in that method?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
New to java, bare with me here. I'm trying to call my method calculatePay into my main method? When I try to do it in my IDE I get a syntax error. When I try to call temp1.calculatePay(hours, payRate, grossPay) I get an error. What am I doing wrong?

Here is my method in a separate class:

Main method in seperate class:


 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Boor wrote:When I try to do it in my IDE I get a syntax error. When I try to call temp1.calculatePay(hours, payRate, grossPay) I get an error. What am I doing wrong?

Error message should tell you what is wrong and in which line wrong things most likely heppened. So, please copy error message and post here exactly as it appears (not a screenshot, just a text).
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Dustin Boor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured out the issue. My error was that the symbol "grossPay" was not found in my main method. I added into my main method and was able to get my desired output without syntax errors. Please let me know if this would be considered "correct" in the java language.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your full working code. There are more problems apart from the one you said. From the calculatePay method's signature I see it requires/accepts 3 parameters (why is grossPay there? whouldn't it compute that anda return back?), but when you call that method, you don't pass any parameters, so that should be an error. There might be more errors - please post working code after you fixed what you fixed.
 
Dustin Boor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is working code:

MAIN CLASS:

2nd Class:
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...calculatePay(40, 7.50, 400);
Now explain why the calculatePay method has a grossPay parameter. That looks like a mistake. Did you add it because of a compiler error?

And welcome to the Ranch again
 
Dustin Boor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was under the impression I had to use all variables I am using for that method as parameters? If I take the grossPay argument out of my calculatePay method, there will be a syntax error in my print statement for that method as I have +grossPay at the end.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't work that way?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you're using static methods, there's no point in creating instances of the TestClass class on lines 6 and 7.

Also, since the method you call is static, it's not a good practice to call it via a reference to an instance object like temp or temp1.

Names like TestClass and temp are not very informative. You should choose names that better reflect their purpose or the idea they represent, like calculatePay, which is a decent name for that method.

Your calculatePay method takes a grossPay parameter when it shouldn't. The grossPay should be a local variable, not a parameter. Parameters are things you need to do something. Grosspay is the value you want to calculate based on the given hours and pay rate.
 
Dustin Boor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I suppose it could work that way :-) Thanks for pointing that out!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can make grossPay a local variable in that method, as an alternative. Are you using Eclipse? Please realise that the many suggestions given when you click the red mark are suggestions. You need to decide which to use, if any.
 
Dustin Boor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can make grossPay a local variable in that method, as an alternative. Are you using Eclipse? Please realise that the many suggestions given when you click the red mark are suggestions. You need to decide which to use, if any.



I am not using Eclipse, I am using IntelliJ. I am starting to see the suggestions now. Thank you.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would appear you get suggestions with all the well‑known IDEs. The code finds itself with a compie‑time error and doesn't know how best to correct it, so it maes several suggestions. That is really useful when you have a spelling errror in the identifier and the suggested corrections include, “Change to errrorr.”
The one case you really need to be aware of is when you get a “resource leak” warning for this sort of code:-
Scanner keyboardIn = new Scanner(System.in);
// ...

If you are reading from System.in you must ignore the suggestion to close that Scanner. Similarly, never close System.out and System.err. Maybe the suggestion to tag the code with @SuppressWarnings is better. You don't get many chances to use @SuppressWarnings. You must close Scanners reading from files, sockets, etc., however.
 
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your calculatePay(int hours, double payRate, double grossPay) method is a static method.
No need of creating an object to access it.

You can call the method by just  writing classname.methodname which in your case is TestClass.calculatePay(hours,payRate,grossPay).
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't intend to be sarastic when I was on about correcting spelling errors. I just put an error in the text and it came out as sarcastic Sorry.
reply
    Bookmark Topic Watch Topic
  • New Topic