HomeJava

Java for Testers – Using Variables

Java for Testers – Using Variables

<<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.

Java for Testers - Variable Demo

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

  1. Local Variables
  2. Instance Variables

Using Variables in Java - Types of 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

Print statements - Java project

2) Inside the main() method, let’s create a variable and assign it with a value 2 as shown below:

Using Variables in Java - local variable

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:

Java for Testers - print variable

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:

Java for Testers - variable value printed

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:

Using Variables in Java - instance variable

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:

Java for Testers - change variable to static

Observe that static keyword will be added before the instance variable and the compiler error will be resolved as shown below:

Java for Testers - static added

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:

Java for Testers - instance variable value printed

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>>

If you like this article, share it on LinkedIn using the below button:

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *