Scope of Variables in Java

Java variables are mainly classified into three kinds:

  • Local Variables
  • Instance Variables
  • Class Variables

1. Local Variables: Variables declared and are used inside methods that are called Local variables. Local variables can also be declared inside program blocks that are defined between an opening brace { and a closing brace }. These variables are visible to the program only from the beginning of its program block to the end of the program block.

2. Instance Variables: Instance variables are declared inside a class, it is created when the objects are instantiated and therefore associated with the objects.

3. Class Variables: Class variables are also declared inside a class. Class variables are global to a class and belong to the entire set of objects that the class creates. Only one memory location is created for each class variable.