Static variable in method.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Static variable in method.

    I m from C and C++ programming world.
    There i saw that a static variable in a method lives between two function calls.
    Right??
    But in JAVA it is valid.
    Look at this code carefully.....

    [code=java]
    class Sample
    {
    //Some code
    void test()
    {
    //Some code
    static int a; //It is not valid ... the compiler error message flashes.
    //Some code
    }
    //Some code
    }
    [/code]

    Plz explain ....

    Kind regards,
    Dmjpro.
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    Originally posted by dmjpro
    I m from C and C++ programming world.
    There i saw that a static variable in a method lives between two function calls.
    Right??
    But in JAVA it is valid.
    Look at this code carefully.....

    [code=java]
    class Sample
    {
    //Some code
    void test()
    {
    //Some code
    static int a; //It is not valid ... the compiler error message flashes.
    //Some code
    }
    //Some code
    }
    [/code]

    Plz explain ....

    Kind regards,
    Dmjpro.
    You can't create static variables in a non-static method. In fact, that's probably the EXACT error that showed up when you tried to compile this... am I wrong? What error did show up? I'm curious...

    Also, the entire idea of a static variable: they're static, which means they are the same for all instances of that class, means that they should be created as instance variables. It only makes sense. You can set what they are in other methods, but you can't create them there.

    Hope that helped,
    -blazed

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Java doesn't implementt that feature. In C/C++ the lexical scope of a local static
      variable is the function itself (it's invisible outside that function), but the variable
      'lives' forever (as long as the process lives).

      You can easily mimic functionally equivalent behaviour using a static class
      variable. The variable can be 'seen' in every method of the class but if other
      functions promise to stay away from it, the functionality is identical.

      kind regards,

      Jos

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by JosAH
        You can easily mimic functionally equivalent behaviour using a static class
        variable. The variable can be 'seen' in every method of the class but if other
        functions promise to stay away from it, the functionality is identical.
        These lines r very tough to understand.
        Plz tell in simple language.
        Plz.

        Kind regards,
        Dmjpro.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by dmjpro
          These lines r very tough to understand.
          Plz tell in simple language.
          Plz.

          Kind regards,
          Dmjpro.
          This piece of code using C/C++:
          [code=c]
          int foo() {
          static int bar= 0;
          return bar++;
          }[/code]

          Can be written in Java as follows:
          [code=java]
          private static bar= 0;
          public int foo() {
          return bar++;
          }[/code]
          Using the Java version the variable bar can be manipulated by other class and
          instance methods; if they 'promise' to stay away from it the functionality of the
          two code snippets is identical.

          kind regards,

          Jos

          Comment

          Working...