This discussion is archived
2 Replies Latest reply: Jun 8, 2007 12:57 AM by 3004 RSS

static local variable?

807600 Newbie
Currently Being Moderated
Hi,

Why a local variable can not be declared as static? What is the logic behind this static keyword?

thks.
  • 1. Re: static local variable?
    807600 Newbie
    Currently Being Moderated
    static members belong to the class, not to the instance(s),
    the class with its (static) members lives in perm space (special kind of heap),
    instances live on the regular heap

    method local variables belong to the executing thread,
    thread local variables are allocated on the stack,
    thread local variables go out of scope when the method completes
  • 2. Re: static local variable?
    3004 Newbie
    Currently Being Moderated
    Hi,

    Why a local variable can not be declared as static?
    What is the logic behind this static keyword?

    thks.
    In C, a static local variable is used to maintain state between function calls. In Java, the way to maintain state is with a member variable. We have no need of static local variables.

    The meaning of the static keyword is "associated with the class as a whole, rather than with any one instance."