Wednesday, February 20, 2019

Lesson 6 – Global variable, local variable

A declaration variable has valid inside open and close bracket contain it.
Create new class name six.
Copy in to class.
static int number=10;
public static void main(String args[]) {     
    
System.out.println("Value of nunmber is : "+number);
         
}
Run to see Console screen show number 10.

Add to main and run again.
int number=8;

Now  screen show 8, variable number in main and number outside are different, the outsider call global variable, insider call local variable.
A variable declare after Class open bracket will be global variable, it will colored blue. Local variables will have black color.
Local variable only valid in it ‘s open and close bracket.
Add in to main.
int b=5;
Add to outside main.
int a=so+b;
Eclipse say it doesn’t know b, because b only valid in main, it has black color mean local variable.

Variable a has blue color because it is global variable.
Now cut and move b to top near first open bracket, it become global variable and has blue color.

When start to learn programme you should not name local variable the same with global variable to avoid mistaken when use them later.

When declare a new variable, if it color blue we know it is global variable, no need to see brackets.

No comments:

Post a Comment