C-style static in Java?

Discussion in 'Java' started by Sierra Bravo, Jul 24, 2005.

  1. Sierra Bravo

    Sierra Bravo Guest

    Hi group
    Is there some way to emulate in Java the semantics of the C-style
    function-level static variable that preserves its value across function
    calls ?

    void show()
    {
    static int i = 0;

    printf("Value is %d",i); // Prints 1.2.3... on multiple calls
    to show()
    i++;
    }

    I can perhaps use an instance variable, but it would be visible from
    all other methods as well (and also creates a dependency on a variable
    outside the method). I tried "static" itself in Java, but the compiler
    complains loudly....

    sb
     
    Sierra Bravo, Jul 24, 2005
    #1
    1. Advertising

  2. Sierra Bravo

    Chris Smith Guest

    Sierra Bravo <> wrote:
    > I can perhaps use an instance variable, but it would be visible from
    > all other methods as well (and also creates a dependency on a variable
    > outside the method). I tried "static" itself in Java, but the compiler
    > complains loudly....


    A field is the way to go. Classes are the unit of encapsulation in
    Java. If you only want to use the variable from one method, then just
    don't use it anywhere else. If the class is big enough that you can't
    easily manage this, then refactor into smaller classes.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jul 24, 2005
    #2
    1. Advertising

  3. Sierra Bravo

    Indudhar Guest

    Would this work?

    class MyCounter{
    private static int i = 0;
    public static void showValue(){
    System.out.println( i++);
    }
    }

    //call to the method
    MyCounter.showValue();

    just curious...
     
    Indudhar, Jul 24, 2005
    #3
  4. "Sierra Bravo" <> writes:

    > Is there some way to emulate in Java the semantics of the C-style
    > function-level static variable that preserves its value across function
    > calls ?


    Yes: Put the function in a final class by itself, and use a private
    variable for what you want.
     
    Tor Iver Wilhelmsen, Jul 24, 2005
    #4
  5. Sierra Bravo

    Alan Krueger Guest

    Sierra Bravo wrote:
    > Hi group
    > Is there some way to emulate in Java the semantics of the C-style
    > function-level static variable that preserves its value across function
    > calls ?
    >
    > void show()
    > {
    > static int i = 0;
    > printf("Value is %d",i); // Prints 1.2.3... on multiple calls
    > to show()
    > i++;
    > }


    In C, a static local variable is initialized the first time the function
    containing it is called. I'm not sure what the multithreading semantics
    of this feature are, though.

    What you'll want is a static field. This will be initialized when the
    class is loaded. Here's some test sample code missing any synchronization:

    public class Foo {
    public static void show() {
    System.out.println( "Value is " + value++ );
    }
    public static void main( String[] args ) {
    for ( int i = 0; i < 4; ++i )
    show();
    }
    private static int value = 0;
    }

    > I can perhaps use an instance variable, but it would be visible from
    > all other methods as well (and also creates a dependency on a variable
    > outside the method). I tried "static" itself in Java, but the compiler
    > complains loudly....


    Since you're in control of the class (make it a final class if you're
    worried), you're in control of what accesses that field. Don't use a
    non-static (instance) field, there will be a separate one for each
    instance of the class. (Unless your C code looks like it's modelling a
    class with instances, but the sample you attached didn't.)
     
    Alan Krueger, Jul 24, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Nicholson
    Replies:
    3
    Views:
    775
    Rob Nicholson
    May 28, 2005
  2. Replies:
    0
    Views:
    2,476
  3. G. Burton
    Replies:
    2
    Views:
    59,916
    Sudsy
    Feb 21, 2004
  4. Aakash
    Replies:
    3
    Views:
    3,947
    Roedy Green
    Jan 19, 2008
  5. Ken Varn
    Replies:
    0
    Views:
    492
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page