Variables in Java
In the previous blog we learned about Non-Primitive Data Types. If you want to learn more about the Non-Primitive Data Types then visit Non-Primitive Data Types in Java. Everyone is aware of the variables. In general terms, variables are nothing but whose values keep on changing. Coming to the Programming Language, variables are generally used to store the values. When the program is executed, values get stored in the memory locations and each memory location has a memory address that is hexadecimal. Remembering hexadecimal addresses is not an easy job, so some names are given to that memory location that is called variables.
Declaring Variables
The general syntax of declaring variables is: data type followed by the name of the variable.
General Syntax:
Note: There should be a semicolon(;) at the end of the declaration. Because a semicolon indicates the end of the statement.
Rules for Declaring Variables
- Variables should start with the letters and be the sequence of letters or digits.
- Variables should not start with the digits nor with any special characters like @, #, etc except underscore(_) and dollar($).
- When declaring the variables, the first letter should always be small.
- Blank spaces are not allowed in the variable names.
- Java keywords like int, char, class, etc. cannot be used for declaring variable names.
- Variable names are case-sensitive.
Initializing Variables
After declaration of variables, we must initialize them by using assignment operators. We cannot use the variables that are not initialized. If we do so then the compiler gives the error.
Output:
- The first way is to assign the declared variable on the left followed by the assignment operator(=) and then the expressions with the values on the right.
- The other way is to declare and initialize it in one line.
e.g.
e.g.
Types of Variables
There are three types of variables:
- Local variables
- Instance variables
- Static variables
Local variables
We declare local variables inside the method. The scope of local variables is limited, i.e. inside the method only, which means we can't change their values and cannot access them outside the method. The local variables must be assigned some values while creation, otherwise we will get errors while compiling the program.
Example without assigning the variable:
Output:
Example with initialization and assignment:
Output:
Instance variables
The variables that are declared in the class but outside the method or constructor are called instance variables. When we create the object using a new keyword then instance variables are created and instance variables are destroyed when the object is destroyed. They can be accessed directly by calling the variable inside the class.
e.g.
Output:
We can also directly access the variables by using methods.
e.g.
Output:
Static Variables
Static variables are also called class variables. Static variables are declared using the static keyword. They are declared outside of the method or constructor in a class. Only one copy is created for each variable in the class and can be used by as many objects as we can. When the program starts, then static variables are created and destroyed when the program stops. Static variables are accessed by using classname.
e.g.
Output:
If we change the value of the static variables then it is affected on each instance of the class.
Output: