The static
keyword has some similar effects in
C and Java, but the
differences can be confusing.
- persistence:
In both C and Java,
values of function-scope variables declared
static
, persist from one function call to the next. - sharing: In C the values are not shared with other running instances of the application, as they are with Java.
- initialization:
C variables declared
static
are initialized to zero. (Non-static variables declared without explicit initializers contain trash.) - association:
The
static
keyword of Java associates a method or variable to its class, rather than class instances.The C
static
restricts the scope of a function or variable to code within the same file.