logo
Variables in Java
Variables in java
A variable provides us with named storage that our programs can manipulate. Each variable in java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
You must declare all variables before they can be used. The basic form of a variable declaration is shown here:
Syntax: data type variable [= value][, variable [= value] ...];

Example: int a;

int a=10,b=20;

Here data type is one of java's data types and variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list.

There are three types of variables in Java:
1. Local variables
2. Instance variables
3. Class/static variables
1) Local variables:
Local variables are declared in methods, constructors, or  blocks.
Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
Local variables are visible only within the declared method, constructor or block.
Local variables
class Test 
{ 
public static void main(String[] args) 
{ 
int a=10; Local variables 
int b=20; 
System.out.println(a+b); 
} 
}
Output :
output:30
2) Instance variables
1. Instance variables are declared in a class, but outside a method, constructor or any block. 

2.nstance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.

3.stance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class. 

4.nstance variables can be accessed directly by calling the variable name inside the class. However within static methods and different class should be called using the fully qualified name: ObjectReference.VariableName.
Instance variables
class Test 
{ 
int a=10; 
int b=20; 
void add() 
{ 
System.out.println(a+b); 
} 
public static void main(String[] args) 
{ 
Test t=new Test(); 
System.out.println(t.a+t.b); 
t.add(); 
} 
}
Output :
output:30 30
3) Class/static variables:
1.lass variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. 

2.tatic variables are stored in static memory. It is rare to use static variables other than declared final and used as either public or private constants. 

3.tatic variables are created when the program starts and destroyed when the program stops. 

4.tatic variables can be accessed by calling it with the class name as follows: ClassName.VariableName. 

5.hen declaring class variables as public static final, then variables names (constants) are all in upper case. If the static variables are not public and final the naming syntax is the same as instance and local variables.
Class/static variables:
class Test
{ 
static int a=10; 
static int b=20; 
public static void main(String[] args) 
{ 
System.out.println(a+b); 
} 
void add() 
{ 
System.out.printl(a+b); 
} 
}
Output :
output:30 30
Calling of static variables
a. Directly possible.
b. By using class name possible.
c. By using reference variable possible.
Calling of static variables
class Test 
{ 
static int x=100; 
public static void main(String[] args) 
{ 
//1-way(directly possible) 
System.out.println(a); 
//2-way(By using class name) 
System.out.println(Test.a); 
//3-way(By using reference variable) 
Test t=new Test(); 
System.out.println(t.a); 
} 
};
Output :
output:100 100 100
Instance vs Static variables
1. Instance variable for the each and every 
object one separate copy is maintained. 
2. Static variable for all objects same copy is maintained. 
One Object change the value another object is affected. 

class Test 
{ 
int a=10; 
static int b=20; 
public static void main(String... venkat) 
{
Test t1=new Test(); 
System.out.println(t1.a);//10 
System.out.println(t1.b);//20 
t1.a=123; 
t1.b=143; 
Test t2=new Test(); 
System.out.println(t2.a);//10
System.out.println(t2.b);//143
t2.b=111; 
System.out.println(t2.b);//111 
Test t3=new Test(); 
System.out.println(t3.a);//10 
System.out.println(t3.b);//111 
Test t4=new Test(); 
System.out.println(t4.a);//10 
System.out.println(t4.b);//111 
} 
}
Output :
output:10,,20,,10,,143,,111,,10,,111,,10,,1111
Variables default values:- Case 1:- for the instance variables the JVM will provide the default values.
class Test 
{ 
int a=10; 
int b; 
public static void main(String[] args) 
{ 
Test t=new Test(); 
System.out.println(t.a);//10 
System.out.println(t.b);//0 
} 
}
Output :
output:10,,0
Variables default values:- Case 1:- for the instance variables the JVM will provide the default values.
class Test 
{ 
int a=10; 
int b; 
public static void main(String[] args) 
{ 
Test t=new Test(); 
System.out.println(t.a);//10 
System.out.println(t.b);//0 
} 
}
Output :
output:10,,0
Case 2:- for the static variables the JVM will provide the default values.
class Test 
{ 
static double d=10.9; 
static boolean b; 
public static void main(String[] args) 
{ 
System.out.println(d);//10.9 
System.out.println(b);//false 
} 
}
Output :
output:10.9,,false
Case 3:- for the local variables the JVM won’t provide the default values before using those variables we have to provide the default values.
class Test 
{ 
public static void main(String[] args) 
{ 
byte a=10; 
int b; 
System.out.println(a); 
System.out.println(b); 
} 
}
Output :
Compilation error:- Variables b might not have been initialized System.out.println(b);