• Post Reply Bookmark Topic Watch Topic
  • New Topic

How To Stop Variable From Resetting Each Method Call ?  RSS feed

 
Rick Tasche
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to write a method to check to see if the current string that is supplied is different than the last one supplied.
Problem is that the string holding the last value ( String Old_Data; )keeps resetting every time the method is called, which is bad ....


 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Welcome to the Ranch!

The first issue is, you should never use == and != to compare String values. They check to see if it's the same object, not the same value. You should use the .equals() method to compare Strings instead.

That might be the cause of what you're seeing. Otherwise, I think I'd need more information. You're deliberately setting the value of Old_Data on line 8, so I assume that's not the problem? How are you actually using the class?
 
Rick Tasche
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is actually dummy code to simplify the real code I am using.

The incoming data ( string New_Data ) is called every time a player moves ... I want it to only fire when the player is in a different area .... the real code condenses 2 integers into 1 string, and is far from complete .... this will be expanded with many calls to hash maps, lists, and Objects

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Well, definitely change NowC != LastC to !NowC.equals(LastC) and see if that fixes your problem, because it definitely won't be working at the moment as you'd like it to.

(Not related to your issue, but I'd strongly suggest adopting standard Java conventions when programming in Java. Among other things, that involves starting all your variables with a lower-case letter).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables do not keep their values between method invocations. That's part of the design of Java. Depending on your specific requirements, you could return the value you want to keep and pass it in next time, or you could set it as a member variable, either of the object whose method you're calling or some other object on which the method operates.
 
Rick Tasche
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Works quite well, thank you. =-) The player name and LastC will eventually both be thrown into a hashmap once I get around to adding the specific player commands ....

Now this business of java not storing values ... how the hay can programs temporarily store generated data ?? Passing values back and forth is a huge waste of resources when one has hundreds of method calls a second, and up to 100 users online at once.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rick I Refuse To Type This Field In wrote:
Now this business of java not storing values ...


Nobody said that. There's a huge difference between "Java doesn't store values" and "Local variables don't store values between method invocations."

how the hay can programs temporarily store generated data


As I mentioned earlier: In objects. That's the first "O" in "OO".

Passing values back and forth is a huge waste of resources when one has hundreds of method calls a second, and up to 100 users online at once.


No, it's not. First, 10,000 method calls per second is nothing. Second, note that we pass references, not objects. Finally, when we choose whether to pass a parameter or store something in our object's state, that decision is made for design reasons, not performance ones.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rick I Refuse To Type This Field In wrote:how the hay can programs temporarily store generated data ?? Passing values back and forth is a huge waste of resources when one has hundreds of method calls a second, and up to 100 users online at once.

How do you know how much of a waste of resources it is? How do you know that it is any performance hit? Further, unless you have specific, documented performance requirements, I don't think most users would really notice the difference between .000001 seconds and .000002 seconds...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Rick I Refuse To Type This Field In wrote:Passing values back and forth is a huge waste of resources when one has hundreds of method calls a second, and up to 100 users online at once.


No, it's not. First, 10,000 method calls per second is nothing. Second, note that we pass references, not objects.


To get a sense of scale, my Windows laptop (which, admittedly is a fairly powerful one, but still, a consumer-grade or business-grade laptop) can make almost 200,000,000 method calls/sec, or about 5 ns/call. A Linux VM running on the laptop can "only" do about 23,000,000 calls/sec, or about 44 ns/call.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!