static var in methods


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: static var in methods

  1. #1
    Join Date
    May 2004
    Posts
    5

    static var in methods

    In C I can do the following within methods:

    Code:
    void methodA(void){
        static int varA = 0;
    
        //perform some check on varA and change if necessary
    }
    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.

    Thank you.

  2. #2
    Join Date
    May 2004
    Posts
    219
    Just make the method static?

    Wait, why do you want to do this?

  3. #3
    Join Date
    May 2004
    Posts
    5
    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center