• Post Reply Bookmark Topic Watch Topic
  • New Topic

how do I save values across methods  RSS feed

 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I'm currently using the get/set method to change values across methods, but every time a new method is called the values reset. Is there a way to save the values across method calls? I hope this makes sense idk.
 
Prasad prap
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post in sample of your code so that your use case make more sense and clear rather than assumptions?
 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok here goes



that's not the real code but the point is I'm calling a method and the value of gold would reset. How do I prevent this?
 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh forgot to create the object but let's just say I did
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we'll need a little more than that. What is gold1's class? Why is upgrades() a static method? Do you really mean for upgrades() to call itself recursively forever?

Whatever gold1 is, assuming its getter and setter methods do the obvious thing (get and set a member variable of type int), your code should print out values decreasing by 5 until you get a run-time error (about 6,000 calls deep, on my machine).
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric. Welcome to the Ranch!

I think you might have to post an example that actually demonstrates the problem (what's known as a SSCEE). There's nothing there that would explain it, but it could be in your getters and setters or in the method you're calling. I suspect we'd see the answer quickly enough with a real example (and you may even find that the exercise of putting together a simple example helps you solve it yourself).
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Mellen wrote:oh forgot to create the object but let's just say I did


Whoa, that might be rather important! Where are you creating gold1? If you create it each time you call upgrades(), you are creating a new object local to each recursively created stack frame, with whatever initial value gold1 assigns to its member variables.
 
Prasad prap
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Eric

This sample of code still is half complete and says very less as others have stated. Edit your post and paste the entire code. Things will be easier for us rather than we guessing on every thing.
 
Maddy Jain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might not be able to come back and read the edited post, but if you're trying to create the object inside the upgrade method as its not an argument, the change wouldn't work outside upgrade(). (Unless the object is static).
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maddy Jain wrote:I might not be able to come back and read the edited post, but if you're trying to create the object inside the upgrade method as its not an argument, the change wouldn't work outside upgrade(). (Unless the object is static).
What do you mean about creating objects in that upgrade() method? There is no object created in that method.
Stevens Miller wrote:Why is upgrades() a static method?
Agre. Making methods static without a good explanation is usually a mistake.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Maddy Jain wrote:I might not be able to come back and read the edited post, but if you're trying to create the object inside the upgrade method as its not an argument, the change wouldn't work outside upgrade(). (Unless the object is static).
What do you mean about creating objects in that upgrade() method? There is no object created in that method.

I'm guessing Maddy means what I meant, since Eric said his (pseudo)code sample left out the object creation.

Really, to be of any use, we'll have to see all of the actual code.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that’s what it means. Thank you.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:So that’s what it means. Thank you.

Well, I'm just guessin'. Until the OP returns with more code, that's all we can do.

Man, these new programmers do come up with some unusual questions. Reading this forum is making me feel like a kid again.
 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



and this is in another class called armycreator



again that's not really the code I used in the program, but it should get the point across, tell me if I need more code or something
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I guessed right! Your problem is exactly what I thought it was: at line 02 in your upgrades() routine, you are creating a new armycreator object each time upgrades() is called. Your code for the armycreator class (btw, it is universal convention that class names start with caps, as in, "Armycreator") doesn't show how gold is initialized (or even what type it is). But, whatever its initial value is, you are getting a new armycreator object, and the same initial value for gold, each and every time upgrades() is entered.

Now, regarding that armycreator code and the initial value (and type) of its gold member, you said this:

again that's not really the code I used in the program, but it should get the point across


No offense to you intended, but a couple of times here people have said you need to provide the actual code you are using, yet you keep posting something else, and saying, "that's not my code, but it's good enough." When you are asking for help in debugging your code, only your actual code is good enough. Trying to help you is hard enough without having to guess at the parts of your code you are leaving out. If you want to trim it down to something just big enough to show your problem, that's fine (it's actually encouraged). But what you post has to be able to run, at least as a complete routine. Shoot, just trimming down your code to the smallest runnable example of your problem will often lead you to the solution on your own.

Anyway, that's your problem: you are creating an entirely new object each time your routine runs, and each of those new objects has the same initial value for its gold member.

Hope that helps.
 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks a lot. It actually is trimmed down code from the program apart from the if statement which I just added so I wouldn't be calling the upgrades method forever.
 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying I should create the object in the Armycreator class? Can I then somehow use a getter in the upgrades method still?
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Mellen wrote:Are you saying I should create the object in the Armycreator class?


No, I am saying you need to create the Armycreator object outside the upgrades() method. (I'm worried that the way you have phrased that question says you are confused about what object-creation actually is. you should step away from static methods and recursive calls for now, I think.)

Now, I am very reluctant to do this, because I think it is likely to reinforce some bad practices if you build upon it, but you can try this in a file called "Example.java":



And this in a file called "Armycreator.java":



That runs and may do what it is you want. However, for it to be of any value to you, you might want to study it long enough to see why it runs the way it does.

Then, I advise the following steps:

1. Forget that the Java keyword "static" exists (other than when it appears ahead of "void main").
2. Forget that methods can call themselves.
3. Study up on what an object is, what it means to use "new" in Java, and how two or more objects of the same class can exist at the same time. (I think a lot of your confusion is arising from the fact that you don't seem to realize your recursive routine is creating multiple instances of Armycreator objects, each of which is referred to by the same name, all of which are distinct from--and independent of--each other, which is all happening because you are using recursion (so see Recommendation 2, above).)

Best of luck.
 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok that's making things seem a lot clearer, I'll do the research now. Thanks for helping a noob like me lol.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!