Variable in Java
What is variable ?
A variable is a name given to a memory location. It is the basic unit of memory storage in a program.
- In Java, all the variables must be declared before use.
A variable is a name given to a memory location. It is the basic unit of storage in a program. - The value stored in a variable can be changed during program execution.
- A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
- In Java, all the variables must be declared before use.
How to declare variable?
Syntax :
data_type variable_name;
data_type variable_name = value;
//Declaring float variable
float simpleInterest;
//Declaring and Initializing integer variable
int time = 10, speed = 20;
// Declaring and Initializing character variable
char var = 'h';Data Type: Type of data that can be stored in this variable.
Variable Name: Name given to the variable.
Value: It is the initial value stored in the variable.
For example, if we write int x = 20, the variable name is x which stores the value 20 in a location of memory, where int is a primitive data type that represents that this variable can store only integer values.
In other words, it can only take integer values. The value stored in any variable can be changed during program execution.
Get kkrajpurohit’s stories in your inbox
Join Medium for free to get updates from this writer.
There are three types of variables in Java:
1. Local Variables
2. Instance Variables
3. Static Variables
class Example
{
static int a; //static variable
int b; //instance variable
public void myMethod() {
int localVar = 42; //local variable
// ...
}
}Let us now learn about each one of these variables in detail.
- Local Variables: A variable defined within a block or method or constructor is called a local variable.
- These variables are created when the block is entered or the function is called and destroyed after exiting from the block or when the call returns from the function.
- The scope of these variables exists only within the block in which the variable is declared. i.e. we can access these variables only within that block.
- The initialization of local variables is mandatory.
2. Instance Variables: Instance variables are non-static variables declared in a class outside any method, constructor, or block.
- As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
- Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used.
- Initialization of the Instance Variable is not mandatory. They are initialized to default values by constructors.
- Instance Variable can be accessed only by creating objects, outside the class in which they are declared.
3. Static Variables: Static variables are also known as Class variables.
- These variables are declared similarly to instance variables, the difference is that static variables are declared using the static keyword within a class outside any method, constructor, or block.
- Unlike instance variables, we can only have one copy of a static variable per class irrespective of how many objects we create.
- Static variables are created at the start of the program execution and are destroyed automatically when the execution ends.
- Initialization of Static Variable is not mandatory. They are initialized to default values.
- If we access the static variable like the Instance variable (through an object), the compiler will show the warning message and it won’t halt the program. The compiler will replace the object name with the class name automatically.
- If we access the static variable without class name, the compiler will automatically append the class name.






