static var in methods
In C I can do the following within methods:
How would I create a static variable within a Java method? I don't want to have to create a class variable and I can't seem to get a static initializer block to work nor am I able to declare a static variable within a method.
static int varA = 0;
//perform some check on varA and change if necessary
Just make the method static?
Wait, why do you want to do this?
The reason that I would like to do this is so that the method remembers what the last setting of one of the variables was between calls. The real problem came about when I attempted to create an SQL PreparedStatement in Java and then each time the method is called I wanted to just use the setString method to change the parameters of the SQL statement. However, I could not get the prepared statement to work and decided for ease just to use the regular create statement. However, that created another problem whereby now I only want to build the statement the 1st time for a particular unit id that I pass to the routine and not change the statement until the unit id changes. Therefore, I wanted to make a static variable of last_unit_id in the method so that I know when the unit id changes.
If I could get the prepared statement to work or this static thing to work that would be great. As an interim solution I made the last_unit_id a private class variable (not ideal).
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center