variablesoopstatic-variables

What's the life span of a variable in a program (in Java)?


Can you tell me how long a variable lives in a program (in Java). i.e. variables declared inside methods, variables used in parameters, STATIC variables, variables used to return from a method, etc.

Thanks.


Solution

    1. References declared inside methods go out of scope when the method exits.
    2. Parameters passed to methods won't be eligible for GC until there are no more references to them or they go out of scope.
    3. Static references are associated with a class and live as long as the class is loaded.
    4. Returned references won't be eligible for GC until there are no more references to them or they go out of scope.