static local variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stmfc
    New Member
    • May 2007
    • 65

    #1

    static local variables

    as far as i know we can declare static local variables in C++,
    cannot we do the same thing in java?
  • Anand Iyer
    New Member
    • May 2007
    • 6

    #2
    Originally posted by stmfc
    as far as i know we can declare static local variables in C++,
    cannot we do the same thing in java?

    Hi

    Whenever you declare a variable it becomes accessible across a class. I dont think you can have a static variable for methods. You can use local variables for methods that live only inside the method. I assume that is what you are meaning by static local methods.

    Thanks
    Anand Iyer

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by Anand Iyer
      Hi

      Whenever you declare a variable it becomes accessible across a class. I dont think you can have a static variable for methods. You can use local variables for methods that live only inside the method. I assume that is what you are meaning by static local methods.

      Thanks
      Anand Iyer

      He is coming from c & c++ background that's why he is asking such a Question.
      Let him understand the Java Framework :-)

      Kind regards,
      Dmjpro.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by stmfc
        as far as i know we can declare static local variables in C++,
        cannot we do the same thing in java?
        Why don't you try it on the compiler and see what happens?
        There's also a few tutorials around (even on this forum) that explain the static keyword in Java.

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by Anand Iyer
          Whenever you declare a variable it becomes accessible across a class.
          That's only true, if this variable was declared globally. If you have something like this
          [CODE=java]
          public void myMethod()
          {
          int var = 3;
          }
          [/CODE]you won't be able to access var outside of that method.
          Originally posted by Anand Iyer
          I dont think you can have a static variable for methods. You can use local variables for methods that live only inside the method. I assume that is what you are meaning by static local methods.
          I think what is meant is something like this:
          [CODE=java]
          public void myMethod()
          {
          static int var = 3; // This is not allowed in Java
          var++;
          System.out.prin tln(var);
          }
          [/CODE]where the output would be:

          4
          5
          6
          7

          when called 4 times. But no, it certainly can't be done in that way and I don't think it can be done at all in Java.

          Greetings,
          Nepomuk

          Comment

          Working...