9. Variable types And Characteristics .
Variable Types
1) Global Variable - Global Variable are declared in class block are known as Global Variable.
They are classified as 2 types : -
1) static Variable
2) Non-Static Variable
2) Local Variable - The Variable which are declared in method block inside method block expect class block called as local variable.
Characteristic of local variable -
1) Variable declared inside a block remain accessible to that block only cannot accessible from outside the block.
If we accessed it from outside block, it will get compile time error.
ex - class example
{
public static void main (String [] args )
{
int a = 20 ;
System.out.println(a) ; // output = 20
}
System.out.println(a) ; // This will give you compile time error
} // Method block close
} // Class block close .
2) We cannot create more than one variable of same name in same method block we can have same name variable in different block.
ex 1 - class example2
{
public static void main (String [] args )
{
int a = 20 ;
System.out.println(a) ;
string a = "hello" ;
System.out.println(a) ;
}
}
// output - we cannot find symbol a ;
ex 2 - class example3
{
public static void main (String [] args )
{
{
int a = 20 ;
System.out.println(a) ;
}
{
int a = 30 ;
System.out.println(a) ;
}
}
}
// Output is 10 20 .
3) We cannot use local variable without initialization because local variable is not assigned with default value as a programmer, we have to assign same value to local variable and then use it.
ex - class var
{
public static void main (String [] args )
{
char ch ;
System.out.println(ch) ;
}
}
// Output compile time error (CTE).
Comments
Post a Comment