<<Previous Post << Complete Tutorial>> Next Post>>
In the previous article, I have explained about using Comments in Java.
In this article, I am going to explain about Variables in Java.
Let’s get started.
Java for Testers – Using Variables
Variable in Java is nothing but a name provided to a reserved memory location.
Let me explain with the below example.
In the above example, value 2 is stored at a reserved memory location in the computer and a is the name given to the memory location. i.e. a is the variable name given to the reserved memory location where the value 2 is stored.
Types of Variables
There are two types of Variables
- Local Variables
- Instance Variables
Demonstrating Local Variable and Instance Variable
Before demonstrating Local Variable and Instance Variable, we have to first find out what is a local variable and what is an instance variable?
What is a Local Variable?
A variable which is declared inside the method is known as Local Variable.
What is an Instance Variable?
A variable which is declared inside the class but outside the method is known as Instance Variable.
Follow the below steps for implementing the local variable and instance variable in Java:
1) Let’s create a Java program and create a Demo class as shown below:
Note: If you are not aware of creating a Java project and a Class in Java, refer to our previous article – Creating a Java project in Eclipse IDE
2) Inside the main() method, let’s create a variable and assign it with a value 2 as shown below:
In the above program, the variable a is created inside the main() method, so we can call it as a local variable.
2 is the value stored in the variable a.
int is the data type of the variable, which will allow us to store integer values into the variable. Will explain more about these data types in the upcoming article.
3) Let’s print the value stored in the variable a using the print statement as shown below:
int a = 2; System.out.println(a);
Save the Project and click on the ‘Run’ button.
Observe that the Program got executed and the value 2 stored in the variable a got printed as shown below:
4) Instead of creating the variable inside the method, we can create inside the class and outside the method to make it an instance variable.
Let’s move the above variable creation outside the method and inside the class as shown below:
As we can see above that there is a compiler error in step 8.
Hover the mouse on the error and select ‘Change to static’ option as shown below:
Observe that static keyword will be added before the instance variable and the compiler error will be resolved as shown below:
static keyword details will be explained in the upcoming article.
Save the Project and click on the ‘Run’ button.
Observe that the Program got executed and the value 2 stored in the instance variable a got printed as shown below:
Here concludes this article.
In the next article, I will explain about using Data Types in Java programs.
Next Steps:
- > To learn more about Java, continue to the next post (Click on Next Post link below)
- > Check complete Java Tutorial Contents here (Click here)
Please leave your questions/comments/feedback below:
Happy Learning ?
Arun Motoori (www.QAFox.com)
On a mission to help the Testing Community in all possible ways.
<<Previous Post << Complete Tutorial>> Next Post>>