kaveer
8/23/2016 - 7:01 AM

Local and global variables

Local and global variables

class Example
{
void firstMethod()
{
int myVar; // local variable
...
}
void anotherMethod()
{
myVar = 42; // error - variable not in scope
...
}
}
{
void firstMethod()
{
myField = 42; // ok 
...
}
void anotherMethod()
{
myField++; // ok
...
}
int myField = 0; // global variable
}
int age = 24; // this is implicit declaration where value is assigned to the variables 
int samAge = age; // this is explicit declaration where value is assigned to variable samAge by another variable age