File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to return values from a different class file to main? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to return values from a different class file to main?" Watch "How to return values from a different class file to main?" New topic
Author

How to return values from a different class file to main?

Ajay Cooper
Greenhorn

Joined: Jan 01, 2013
Posts: 2
Hello,
After a relatively small C++ background, I decided to learn some Java. I want to be able to pass two variables from my main class, evaluate them in another class, and return that answer to the main class. I have all the code done for it pretty much, but I can't figure out how to use the returned variable in my main class. Any help would be appreciated!

I just tried adding a "System.out.println(answer)" in the case inside the switch statement, but I got an error saying that "answer cannot be resolved to a variable." I'm sure this is a quick fix, but I couldn't find anything helpful by googling it, so any help would be awesome.
Thanks much!
~Carpetfizz
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 72

It's because you don't have the variable "answer" declared anywhere.

Also because you declared the add method static, you don't have to create an Addition object.

So instead of having to do:


Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Ajay Cooper
Greenhorn

Joined: Jan 01, 2013
Posts: 2
Thank you very much for the answer!
I learned that I could also just do:
, and it will print out returned value of that method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Welcome to the Ranch

You have fallen into a trap which lots of people fall into. You are writing too much code all in the same place. You are going to write enter 1st number enter 2nd number four times. That is not at all good style. That ought to be moved somewhere else. A separate method. Maybe even in a separate class. Create an Arithmetic class with two fields for the two numbers, and sum() difference() product() quotient() (=divide) and remainder() methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to return values from a different class file to main?