Solved

keeping the value of an int

Posted on 2004-03-29
16
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

728 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