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 passing variables between main and a test class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "passing variables between main and a test class" Watch "passing variables between main and a test class" New topic
Author

passing variables between main and a test class

Steven Hofmann
Greenhorn

Joined: Oct 24, 2010
Posts: 17
Hey guys,

I'm working on a larger project for a class. It requires me to have a main.java, and create a class in a separate file. I wrote a big program up but am having trouble passing variables to the class, modifying them, and then returning them to the main.java. I thought I could pass and return variables like I normally do for any method contained in the main.java, but it is not working. To try and figure this out for myself, I created another simple program to see what I could do to get variables passed from main.java and the testclass.java as shown below.

Can you guys help point me in the right direction how to get the class to modify the "i" variable, then return it to main.java? The output of this program just keeps repeating "5" for every status and result.

Thanks!

Steve



Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26173
    
  66

Steve,
You are really close!

Consider how you are calling this method.


You are calling the method which returns the new value of i. Then you are ignoring the return value. What if you set it in the main method to the existing i variable.

To make this a bit clearer, I've renamed i to temp. Now in main, we have "i" and in testclass, we have "temp". This makes it more obvious we are dealing with two variables. And Java has no way of knowing you want to assign iminus(i) back to i.

package assignment7_testclasses;



[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26173
    
  66

Also, you didn't ask this, but I'd like to mention that "testclass" should be named "TestClass" to follow standard Java conventions.
Steven Hofmann
Greenhorn

Joined: Oct 24, 2010
Posts: 17
Hm, interesting. Are you suggesting that I create another variable called temp, and set "i = temp" in the main.java? I'm still a tad lost.

When you say "it ignores the 'i' coming back form the class," I guess my trick is getting the main to recognize that it is new... but how do I get the main to recognize the temp that is coming back?

What I tried is creating "int counter = i;" in the main, then sent the counter to the method. the method is set to return 'counter'. then in the 'while' loop in main, i told it to make i = counter, but it still doesn't work. Maybe my logic or way of doing things is very far off?



To give you an idea of what is going on, I've got the ubiquitous "vending machine" as one of my final programs to write for the semester. I made a flowchart and laid it out all the different things to do in methods in a separate class file. It works beautifully, but none of the variables are going back and forth between the main.java and class file.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26173
    
  66

Steven,
No. I was trying to rename one to temp so it would be more obvious they were separate variables. That didn't work out as I intended.

Consider the difference between the following two lines:


What is the difference between the two? Why does it matter?

I'm asking a lot of questions to make you think about rather than just saying the answer. I hope this makes it more memorable and easier to understand in the long run.
Steven Hofmann
Greenhorn

Joined: Oct 24, 2010
Posts: 17
Jeanne... Thanks for that. I like your approach. No, I definitely don't want the answer. The tough thing about online classes is I don't have any examples to go on other than google and the book (which, I must add, isn't exactly inspiring LOL).

I think you just flipped a bulb in my head. Results soon, I hope!

Thanks!
Steven Hofmann
Greenhorn

Joined: Oct 24, 2010
Posts: 17
Ah, I see what's going on now. It didn't cross my mind about assigning a value by calling a method... Is that how you would word that? I think I need to re-write my vending machine program from scratch, but maybe this time it will work for a change!

Too bad I can't buy you a drink over the internet, lol.

Thanks again!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26173
    
  66

Steven Hofmann wrote:A It didn't cross my mind about assigning a value by calling a method... Is that how you would word that?

I would word it as assigning the result of the method to a value. But you get it now and that's the important thing.

Steven Hofmann wrote:Too bad I can't buy you a drink over the internet, lol.

If you have the opportunity to link to the ranch, it's a great thank you to all the helpful folks here. (see the social networking icons on the bottom of coderanch.com for an easy way to do so.)
Matthew Mellott
Greenhorn

Joined: Nov 29, 2010
Posts: 14
Look into variable scope. The i in your main method and the i in the other class exist in two completely different scopes based upon where they are declared.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: passing variables between main and a test class
 
Similar Threads
Need Some Help with a Program
Cannot find main class?
Variables from another class won't assign to main class
Netbeans 7.0.1 questions
Sams Java in 21 Days java.lang.NoClassDefFoundError: main