TYPES OF VARIABLE
Local variable:
Variable created inside global area is known as global variable.
Variable created inside the local area is known as local variable.
Rules for local variables:
- We can not used loacal variables without initialization.
- Local variables are not assigned with default values.
- We can not create two variables with same name.
- Local variable can be used only inside local area, but we can not used inside global area.
Note: Global variable can be used inside global area as well as local area.
Program: ( with compile time error )
class P1
{
public static void main(String [ ] args)
{
int a; //compile time error
System.out.println(a);
}
}
Program: ( with compile time error )
class P1
{
public static void main(String [ ] args)
{
int a=10;
int a=20; //compile time error
System.out.println(a);
}
}
Program: ( with compile time error )
class P1
{
public static void main(String [ ] args)
{
int a=10;
int a=20;
}
System.out.println(a); //compile time error
}
0 Comments