Day7: Type of Variable in Java
Type of Variable in Java
In Java we've three type of variable:
- Local Variable
- Instance Variable
- Static Variable
1)Local Variable
These are those variable which are declared inside a method and they do not have any default value. so, it is an error to use an uninitialized local variable -
class Demo
{
public static void main(String [ ] args)
{
int a = 30; //Local Variable
System.out.println("The value of a is "+a);
}
}
2)Instance Variable
These are those variable which are declare at the class level and they have default values alloted by java which are :
byte, short, int ----> 0
char ----> '\0'
float ----> 0.0f
double ----> 0.0
long ----> 0L
boolean ----> false
referance ---> null
class Demo
{
int a; //Instance Variable 'a'
public static void main(String [ ] args)
{
System.out.println("The value of a is "+a);
}
}
3)Static Variable
They also are variable declared at the class level but prefix with keyword static. They have same default value of instance variable.
class Demo
{
static int a; //static Variable 'a'
public static void main(String [ ] args)
{
System.out.println("The value of a is "+a);
}
}
Comments