A variable is a name of the memory location which is used by the program to deal with the
logic in storing values of different data types.
There are different types of Variables and are classified as:
- Local variables
- Instance variables or Non – Static Variables
- Static Variables or Class Variables
- Constant Variables
- Readonly Variables
A variable declared within a block or method or constructor is called a local variable.
These are also used in “for, switch, for each, catch and using statements”.
The instance variable is a variable defined in a class and the instantiation of the class is called
an instance variable or object of the class and each instantiated object has its own copy,
or instance.
The output will be:
Static variables are called as Class variables. The variable declared in a static block or variables with a static keyword is called Static
Variables.
Note: To access the static variables, we need to use the class name prefix instead of
the object of the class. So, objects are not required for static class or static variables.
ClassName.VariableName
The output will be:
Variables using the keyword “const” are Constant Variables. These variables should
need to assign value during the declaration time.
The values assigned to this variable are constant and cannot be changed.
Note: To access the constant variables, we need to use the class name prefix instead
of the object of the class.
The output will be:
Variables using the keyword “ReadOnly” are ReadOnly Variables.
These variables do not need to assign value during declaration but they cannot be
changed like constants once assigned a value.
The output will be: