Link to home
Start Free TrialLog in
Avatar of poiu77
poiu77

asked on

keeping the value of an int

Each time a button is pressed i am calling a method from a seperate program.
The program looks something like below,the method go() is what im calling. In the method go, i change the value of "sent" a few times with if statements. However each time i press the button, the go method is recalled and "sent" has been set back to zero. I want "sent" to remain as the last value i set it to in the go method.
Can anyone help?>


public class go
{
public int sent;

public go()
{



}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Then you must have 'sent' as an instance variable of the class. It should be private
Avatar of poiu77
poiu77

ASKER

im not sure what you mean.

ive change it from
public int sent;

to
private int sent;

but same problem
Sorry, i slightly misread your question. On the face of it, there shouldn't be a problem

private int sent;

public int getSent() {
      return sent;
}

public void go() {
      // somthing or other
      sent++;
}

..........

yourClass.go();
int sent = yourClass.getSent();
yourClass.go();
int sent = yourClass.getSent();

//etc.
Avatar of poiu77

ASKER

I dont think you know what i mean. Each time i run the go() method the value of the int "sent" determines what happens within the method( due to if statements). The first time i run the method, its ok, because int is set to 0; by the end of the method however int is set to 1. The next time i run the method, it should give me a different result, because int is now 1. But it doesnt, each time i press the button, the method is called and the int is set back to 0;

To assist with what CEHJ is saying, your variable is back to zero because it is declared as a local variable (it is in scope only inside that method) so when the method returns the memory for 'sent' is released.
 (re-calling the method declares a new integer).

   You need to make it as CEHJ shows above, where it is declared as an instance variable and so remains scope for the whole instance. ie. when you return from the method, as long as the object still exists in memory then you will have access to it.
The interesting thing is twobitadder has echoed my first comment. The reason i changed my mind is that in this example you posted:

>>
public class go
{
public int sent;

public go()
{



}
>>

the variable sent IS an instance variable. However, i suspect that either:

a. the code is not actually as  you posted, or
b. there is another, local, variable named 'sent' that's covering up the instance variable


Avatar of poiu77

ASKER

the code is as i posted.
Avatar of poiu77

ASKER

the code is as i posted.
ASKER CERTIFIED SOLUTION
Avatar of rukumam
rukumam

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please post the actual code
>> the method go() is what im calling

So,
1) your class is called go?  (btw, in java classes better start with an upper case, Go)

2) you are calling the method go()
   
    Do you mean you call the constructor? How?
    By performing a

           new go()       ?

   Then it's normal that your variable sent is set back to zero.
   In fact it isn't. You just created a new instance of the class and so the "sent" variable is initialized at 0.

Could that be possible?
I miss-parsed the method, since there is a class called go and a constructor called go, with no real method.

To clear up my echo of your SECOND comment, I was attempting to clarify the reason for using an instance variable rather than a local variable.
Avatar of poiu77

ASKER

znnyx your too cocky for your own good. Ive just solved the problem thanks to rukumam. All i had to do was set the variable to static as below.

private static int sent;

Thanks CEHJ and rukumam.
Avatar of poiu77

ASKER

i made a mistake when entering the code originally. It looked more like this.

public class go
{
public int sent;

public go()
{
//no code in here at all
}
public String reply(Vector enter )
{
//a lot of if statements where the int is changed. This is the method i kept recalling and everytime i did "sent" was back to 0.
}
}
LOL - whatever you'd like to say about zzynx, he's actually guessed precisely what was happening. I'm not sure why you'd repeatedly call the constructor and trap the value in a static variable, but it sounds like a mighty bad idea...
>> whatever you'd like to say about zzynx, he's actually guessed precisely what was happening.
Thanks CEHJ.  That's exactly what I did.
Sometimes we get bad info (or not even half the info) and nevertheless we try to help.

And poiu77, before you start flaming, just think that we are trying to help you.
But OK, if you think I'm cocky I'll note that.
There are enough others that can help you out.
I'll won't spoil my precious time in trying to decode your (even wrong) info and answering it.