What Are Variables In Java?

Vijay Ingle
4 min readNov 9, 2021

--

Hello friends, Welcome to CodeOuter. Today I am going to explain to you what is variables and why we need variables and what are the types of variables. I will explain in detail through examples.

Let’s get started to learn.

Table of Content :

  • Objective of blog
  • What is a variable?
  • What are the types of variables?
  • Examples of variables
  • Conclusion

What are variables?

A variable is used for holding the name or value of a memory location. It means the variable is just like a container that has value in it. for example, we have a box and pen here box is a container that contains a pen in it. In the same way, the variable holds value while executing the java program. I hope you understand well now, look at different types of variables.

What are the types of variables?

We have three types of variables. it is used for a different purpose. let me explain to you.

1. Local variable

2. Instance variable

3. Static variable

Local variable: It is a variable that is available inside only in the body of the method. scope of local variable is only within the method. even the class does not know whether this variable exists or not.

I nstance variable: It is a variable that is available inside a class of body and outside the method .it is accessed through the object of the class. we can’t directly access it.

Static variable: It is a variable that is available inside the class of body and outside method. It doesn’t require any object to access the value of it. we directly access it.

Now, let’s look at the example of it.

Example of variables:

1. Local variable :

class Student{ public static void main(String[] args) { int student_id = 101; // local variable System.out.println(student_id); } }

Output: 101

The student is class and we have student_id is a local variable inside the main method which holds value 101.

you may have a question about why we write student_id inside the main method. why do we not write in outside method? let’s answer this question.

If you write outside of the main method then student_id is not accessible inside the main method. it gives an error. okay, but if we want to use it outside the how can we use it. now the instance variable comes into the picture.

2. Instance variable:

class Student{ int student_id = 101; // instance variable public static void main(String[] args) { Student s = new Student(); System.out.println(s.student_id); } }

Output: 101

The student is class and student_id is an instance variable that is inside the class but an outside main method. you can see instance variable is not directly accessed in the main method. we need objects through which we can access the value of a variable. new is the keyword used to create the object of the class. The student has “s” object through which we access the value of student_id. it seems to be simple right.

but wait.. what if I don’t want to use that object. I want to directly get the value of the instance variable even if it is outside the method. now static variable comes to the picture.

3. Static variable:

class Student{ static int student_id = 101; // static variable public static void main(String[] args) { System.out.println(student_id); } }

Output: 101

The student is class and student_id is a static variable that is available inside the class and outside method. you can directly get the value of the variable no need to create an object of a class.

Here is another question.. can we write static variables inside the main method. the answer is no it gives an error.

I hope you understand well. now look at one example in which we use these three variables together.

Quiz of the day:

Let’s say we have Student Class and we want student property like roll number, name, marks. use these three variables and the access value of it.

class Student{ static int stud_roll_no = 101; // static variable String stud_name = "Ajay"; // instance variable public static void main(String[] args) { float stud_mark = 98.5f; // local variable Student s = new Student(); System.out.println("Student roll no : " + stud_roll_no); // get static variable System.out.println("Student name : " + s.stud_name); // get instance variable System.out.println("Student marks : " + stud_mark); // get local variable } }

Output: Student roll no: 101 Student name: Ajay Student marks: 98.5

Conclusion:

I hope this blog is giving detailed information about variable how exactly variable works and the types of the variable through examples. If you don't know about JDK, JRM, JVM Click Here.This tutorial if you like please do comment. we assure you that you gonna not find any problem with the post. If you have any doubts feel free to contact us or post a comment. we will surely revert back.

I will see you in the next post.

Originally published at https://codeouter.com on November 9, 2021.

--

--