Passing values (although it may not be as simple as the title suggests)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nobody123
    New Member
    • Feb 2008
    • 2

    Passing values (although it may not be as simple as the title suggests)

    Hello there, thank you for taking the time to read my question. Anyways, on to the matter. I have two files called "test" and "test2".

    Here's the code for "test":
    Code:
    import java.util.Scanner;
    
    public class test
    {
    	private double amt;
    	Scanner input = new Scanner( System.in);
    
    	public void setAmt()
    	{
    			System.out.println("Input number: "); //User imputs number here
    			double wagerAmt = input.nextDouble();
    			amt = wagerAmt; //Assigns wagerAmt to amt
    	}
    
    	public double getAmt()
    	{
    	return amt;
    	}
    }
    And here is the code for "test2":
    Code:
    import java.util.Scanner;
    
    public class test2
    {
       public void printNum()
       {
    	test a = new test();
    	System.out.println(a.getAmt()); //Prints amt from file "test"
       }
    
    	public static void main(String args[])
    	{
    	test a = new test();
    	a.setAmt(); //calls method setAmt() from file "test"
    	test2 b = new test2();
    	b.wagerNum(); //calls method printNum() from above method, although this does not function properly.
    	}
    }
    What I was hoping for when I did this is that the value of "amt" from file "test" would be passed down through the getAmt() method and its value would be successfully printed out using method printNum(). Although this is not the case, because the output states that it re-initializes back to zero. Say for example I entered the number "2" when prompted. This is the output:

    OUTPUT:

    Input number: //Prompt to input number
    2 //The value of amt in file "test"
    0.0 //The value of amt in file "test2"
    Press any key to continue . . .

    In other words, I would like the value of amt in test one to be the same as the one in test2. I am relatively new to Java and I am sorry if I bother anyone with something that "should" be incredibly obvious. Any suggestions would be greatly appreciated. Thank you.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Your problem is that you create separate instances of your test object in each method. Since amt is not a static variable, it is not shared between all instances of an object and each object has its own version of amt, which is set to something different. To change this, declare amt with the 'static' keyword in the first file. This means that only one copy of amt exists, no matter how many instances of test you create.

    Comment

    • nobody123
      New Member
      • Feb 2008
      • 2

      #3
      Thank you so much, Laharl! I have been at this for days on end and it was such a easy question too. Thanks to you, I can finally perfect the program that I am currently writing. Thank you again!

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by nobody123
        Thank you so much, Laharl! I have been at this for days on end and it was such a easy question too. Thanks to you, I can finally perfect the program that I am currently writing. Thank you again!
        Making data "static" is a quick and dirty fix but is almost always the wrong solution. Data is static only if it is logically wrong for there to be more than one instance of it. In your case it's not logic but a matter of convenience.

        Comment

        Working...