Java variables | Class variable in java | Types of variables in java

Aap Ki Apni School
2 min readJan 27, 2021

In this blog we will discuss What is Java Variable?How to use Java Variables?Types of variables in java in details.

Java variables | Class variable in java | Types of variables in java

What is Java Variables?

Java variable is a tempory mempry where we can store values. Name of that variable is the reference of that memory area. With the help of reference we access the vales of that variable.

int x=10;

int: int is type. Type is mentioning which type of data we can store. In above given example we can store only Integer value,not other than Integer.

x: This is the variable name.

10: This is the value that stored in memory for temporay purpose.

Global Variable:

Global variable is a variable that is declared on place and it is accessible in entire code. But Java is a object oriented language where everything is Class. We can declare variables on class name like static or instance variable.

Types of variables in java

Java Variables are three types:

1. Local Variables

2. Static Variables

3. Instance Variables

Local Variables:

Local variables are the variables which are defined inside any method or block. It is accessible only in that block or method. We can’t define static variable inside method.

Static Variables:

Static variables are those variables which is defined on class level with static keyword. These variable can be access by class name. Static variable defined only once for all objects. It consume memory only once.It doesn’t matter hoe many objects we are creating. As this is common for all object if any object change vale of this variable then it will impact all objects.

Instance Variables:

A variable declared inside class and outside method is called Instance Variable. Instance variable belongs from Object. It can be access by Object. For each object instance variables take different different memory. If one object change value of varaibles then it will not impact other objects.

Program for Types of Variables:

class Test{
int x=50;//instance variable
static int y=100;//static variable
void method(){
int n=90;//local variable
}
}//end of class

Let’s Discuss More Topics

Please like and comment if you like this story.

--

--

Aap Ki Apni School

I am a software developer. I have been working in IT since 5 years. I am interested in learning new things.