Solved

keeping the value of an int

Posted on 2004-03-29
16
176 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now