944,131 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2519
  • Java RSS
Nov 14th, 2004
0

problems with referring to class variables

Expand Post »
I'm having a problem with a GUI i'm building. I have a separate class called Calculations.class which does nothing except hold certain variables and do calculations with them. One of my classes requires a user to enter numerical data, and when a button is pressed, these variables are set in calculations.class, which is fine. However, when I try to access these variables in Calculations.class from another class, it seems like all the variables are set back to zero, and do not have the values set by the user originally. Any help would be fantastic!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
matt_5104 is offline Offline
15 posts
since Nov 2004
Nov 14th, 2004
0

Re: problems with referring to class variables

It sounds like a scope issue where the Calculations object whose values you're setting is being destroyed and then recreated. When you access the values, they're fresh from the constructor.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 14th, 2004
0

Re: problems with referring to class variables

Hummm, you want an answer without posting any code? Like fixing your car when you left it at home.......
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Nov 17th, 2004
0

Re: problems with referring to class variables

Your Problem is Scope of variables. Please use static variable which are class variables and retain there values. If U want to use instance variables then U should be careful about the Object which get created when user clicks the button. U can not refer the same object in different file. At that time it creates new instance of that class and hence valued U get will be default. If U want to use in different Independent class then U have to pass these variables to new class thorogh constructor.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tozaheer is offline Offline
1 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java DSW algorithm Coding
Next Thread in Java Forum Timeline: Setting preset for a JMF Player





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC