473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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":
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class test
  4. {
  5.     private double amt;
  6.     Scanner input = new Scanner( System.in);
  7.  
  8.     public void setAmt()
  9.     {
  10.             System.out.println("Input number: "); //User imputs number here
  11.             double wagerAmt = input.nextDouble();
  12.             amt = wagerAmt; //Assigns wagerAmt to amt
  13.     }
  14.  
  15.     public double getAmt()
  16.     {
  17.     return amt;
  18.     }
  19. }
  20.  
And here is the code for "test2":
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class test2
  4. {
  5.    public void printNum()
  6.    {
  7.     test a = new test();
  8.     System.out.println(a.getAmt()); //Prints amt from file "test"
  9.    }
  10.  
  11.     public static void main(String args[])
  12.     {
  13.     test a = new test();
  14.     a.setAmt(); //calls method setAmt() from file "test"
  15.     test2 b = new test2();
  16.     b.wagerNum(); //calls method printNum() from above method, although this does not function properly.
  17.     }
  18. }
  19.  
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.
Feb 14 '08 #1
3 1120
Laharl
849 Expert 512MB
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.
Feb 14 '08 #2
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!
Feb 14 '08 #3
BigDaddyLH
1,216 Expert 1GB
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.
Feb 14 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: rick | last post by:
Does anyone having a working example of passing values between web forms pages? I tried the example in the ms- help but I can't get by the following: To read property values from the source...
2
by: Jim Heavey | last post by:
I know that I can create session variables and pass those values from one screen to another, but is there not a way to makes variables from one scree n available to another screen other then...
2
by: VS_NET_DEV | last post by:
Hi All, There two ways of passing values between pages - 1) Context.Items.Add 2) Context.Handler (cast the page1 class etc) I know for sure #2 does not store the values between requests. How...
1
by: olduncleamos | last post by:
Hello all. With a background in ASP, I am finding the work required for passing values between pages mystifying. For various obvious reasons, I have eliminated using cookies and session to store...
2
by: Ganesh | last post by:
Hi there, I'm new to .net just started with 2005 and asp.net 2.0, I'm passing control values to other page to retrieve the data from data set based on the passing values. It works fine. But when...
1
by: Steve | last post by:
After a few hours of trial and error I have reached the following conclusions, can you please tell me if I am right: I have 2 aspx pages both with the same master page and I wish to pass values...
2
by: johnj | last post by:
I am converting an application I wrote in VB.NET to C#. I am not a C# guy, at all. I am having a problem passing values to a property in C#. I understand that it is probably not the best way of...
1
by: swapna | last post by:
hi, in my project,i have problem i.e dynamically should pass values to checkboxes.i am using Jsp technology/html.in html i created checkbox but passing values to that checkbox dynamically in...
3
by: Archanak | last post by:
Hi, Any limits exists while passing values of checkbox to another program? Here is the code: <script type="text/javascript"> function valuate() {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.