Solved

keeping the value of an int

Posted on 2004-03-29
16
180 Views
Last Modified: 2010-03-31
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()
{



}
0
Comment
Question by:poiu77
  • 6
  • 5
  • 2
  • +2
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10705292
Then you must have 'sent' as an instance variable of the class. It should be private
0
 

Author Comment

by:poiu77
ID: 10705533
im not sure what you mean.

ive change it from
public int sent;

to
private int sent;

but same problem
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10705630
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.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:poiu77
ID: 10705839
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;

0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10705964
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10706799
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


0
 

Author Comment

by:poiu77
ID: 10707225
the code is as i posted.
0
 

Author Comment

by:poiu77
ID: 10707226
the code is as i posted.
0
 

Accepted Solution

by:
rukumam earned 60 total points
ID: 10707257
Did you try declaring sent as a static variable??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10707289
Please post the actual code
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10707465
>> 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?
0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10707629
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.
0
 

Author Comment

by:poiu77
ID: 10707649
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.
0
 

Author Comment

by:poiu77
ID: 10707708
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.
}
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10707904
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...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10711334
>> 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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to install java on RHEL image on EC2 4 41
hibernate example using maven 12 59
Html Table Looping (part 2) 5 28
java mysql insert application 14 29
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question