1.6 Local & Global Variables.
Local & Global Variables:
{
-
- //declare variable(scope and variable is upto the context.)
-
}
eg: {
int x;
x = 10;
System.out.println(x);
}
System.out.println(x); //error
{ //outer context
dclare variable; //global variable
{ //inner context
declare variable; //local variable
}
}
eg: {
int x = 10;
{
int y = 20;
System.out.println(x);
System.out.println(y);
}
System.out.println(x);
System.out.println(y); //error
}
Notes :
1. The variable declare in a context ristricted to that context only.
2. The scope of the variable is limited up to the context it can not be access from other context.
3. A variable declare in a outer context can be access in the inner context also.
4. The variable declare inside the inner context should be accessed only inside the inner context can not be accessed in outer context.
5. if the inner context variable (local) and the outer context variable (global) name are same than JVM used prefrence to the inner context variable.
eg:
{
int x = 30;
{
x = 60;
int x = 90;
System.out.println(x); //90
}
System.out.println(x); //60
}
for more details please follow video given below:
Your best blogs topic's will be good traffic
ReplyDelete