Results 1 to 14 of 14

Thread: Static keyword...what does it mean...

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Question Static keyword...what does it mean...

    I'm not a Java programmer. I use VB, but I have to do an assignment in Java. And I have problems about when to use static, and when not to use it. I really don't understand what it means. So can you please use all your forces to try to explain what it means? What is a Static variable, and what is a static method and so on....

    Pleas explain like if I was 5years old or something like that...

  2. #2
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    A static variable contains the same value across all instances of a class. For an example, if you have a class, call it myClass, which contains a non static variable myVar and a static variable statVar. Now if you have two instances of the class, ie
    myClass var1 = new myClass();
    myClass var2 = new myClass();

    two different portions of memory are reserved for myVar, but only one for statVar. In other words, whether you reference statVar from var1 or var2, they will both return the same result, no matter what you have changed in them. On the other hand, if you reference myVar, it will contain the data that the specific class put in it.

    A static procedure is a procedure that doesn't change any non static variables. It is a procedure that can be called without instantiating the class first. The entire Math class consists of static procedures. I think they call such classes utility classes.

    Hope that helps

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Thanks that was very informing. It was really easy to understand what you meant, even for me who aint that god in English.

    Thanks again.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Does Java have static local variables? It would be another meaning of the static keyword.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I'm not sure....does anyone else know...what do you exactly mean my local variable....that it is in a procedure or something like that????

  6. #6
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    No. Local variables are only static if the procedure is static. You cannot declare a static variable inside a procedure.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What I mean is this C function:
    Code:
    void somefunc(void)
    {
      static int calls = 0;
      printf("Call No. %i\n", ++calls);
    }
    Is this possible in Java?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    No, as I said, you cannot use the static keyword inside a procedure. To make a static variable the entire procedure must be static:
    Code:
    private static void somefunc()
    {
         int calls = 0; //this var is static
    }
    
    private void somefunc()
    {
         static int callls = 0; //this is a syntax error
    }

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    private static void somefunc()
    {
    int calls = 0; //this var is static
    }
    That doesn't make sense. static member functions and static local variables use a completly different meaning of static, so it would be a HUGE language flaw if it were so.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by marnitzg
    Code:
    private static void somefunc()
    {
         int calls = 0; //this var is static
    }
    I don't see how this could be. Since you must initialize a variable before using it, wouldn't the value be getting reset every time the method is called anyway?

    I thought only global variables could be static.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  11. #11
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    Hmm, think you're right. Reasons:
    1. 2 threads running the same procedure
    2. Recurring functions would lose information

    Therefore I think you're right, only global variables can be static

  12. #12
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    Originally posted by crptcblade
    I don't see how this could be. Since you must initialize a variable before using it, wouldn't the value be getting reset every time the method is called anyway?
    So according to this logic, a static global variable would also be useless.

  13. #13
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by marnitzg
    So according to this logic, a static global variable would also be useless.
    Well, no, I may have stated it wrong. The life time of a global variable is the life time of the class, and the life time of a static global variable is the life time of the calling program.

    On the other hand, a local variable has a life time of the procedure only, in Java anyway. So when a global variable is initialized, it is still initialized the next time it is accessed. But a local variable would need to be reallocated, and therefore reinitialized everytime you call the procedure it is in.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    One of the most confusing things about C++ is that the static keyword has 3 different meanings:
    Meaning a:
    Code:
    static int someGlobalVar;
    static void someGlobalFunc;
    static here means that the variable/function is local to the code module. It is not exported by the linker and cannot be referenced from other modules. This avoids ambiguity errors.

    Meaning b:
    Code:
    void someFunc(void)
    {
      static int statVar;
    }
    This variable is static to the function. It's the meaning that most accuratly hits the meaning of the word 'static'. It doesn't lose it's value between calls to the function. It's like a global variable that can only be accessed from within the function it's declared in.

    Meaning c (new to C++):
    Code:
    class A {
    public:
      static int s_iVar;
      static void Func(void);
    };
    static here means that the variable/function belongs to the class instead to it's instances. This is like in Java. Can be accessed without an instance (in C++ you use the scope resolution operator for this: A::s_iVar = 3; A::Func();


    Meaning b is useless in Java, but what about meaning a? It is very useful, and I wouldn't like having to define class variables for a thing I only want accessible from a single function.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width