Solved

keeping the value of an int

Posted on 2004-03-29
16
177 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordmultiple challenge 12 116
solarwind tftp server 2 44
Spring Framework HTTPSession management 1 21
object oriented programming comparison 5 52
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …

929 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

11 Experts available now in Live!

Get 1:1 Help Now