Scope of Variables in java

Instance and Static Variables : The scope of variables belongs to instance  is entire class. The scope of static variables is also entire class. They can be declared any where in the class.

Local Variables : The scope of local variables start from its declaration and remain valid till the end of the block that contains the variable.

Note :

    • A local variable must be initialized explicitly before it is used
    • A local variable can be declared multiple times with the same name in different non-nesting blocks in a method
    • A local variable can’t be declared multiple times with the same name in nested blocks
local variable

Scope of Local Variables

Scope of varaibles in non-nested vs nested block

Scope of varaibles in non-nested vs nested block

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.