Static Variable and Local Variable in Java

This “Static Variable and Local Variable in Java” post explains clearly how to access static and local variables and why jvm access static variable if local variable is not present inside block or method.


 

Local Variable :

  • Declaration of variable inside block or method.
  • We can access local variable directly.

 

Static variable :

  • Declaration of variable outside to blocks and methods.
  • We need to access static variable using class name.

 

Note:

  • If we access local variable and it is not present, it access static variable.
  • It is recommended to follow syntactical rules while accessing local and static variables.

 

 

  • If local variable is present, we cannot access local variable directly.

 

Share this