Dart Variable Scope

Add Your Heading Text Here

The scope of variables is the lifetime of the variable.

The variable scope is the lifetime of a variable for which a particular block of code is executed.

Variable scope can be decided using the type of variables,

  1. Local Variable
  2. Global Variable

Local Variable

A local variable is a type of variable declared inside programming blocks(i.e., for loop, if else, switch and many more) or functions.

It can only be used inside that particular code of block or function in which it is declared or defined.

Once that particular code of block or functions is executed, the scope of a variable is finished or say that the local variable is destroyed.

For Ex :

void main()
{
       int x=1;
       for(int i=0;i<2;i++)
       {
    	 print(i);
	 x++;
	 print(x);
       }
}

Output :

0213

In above example, the scope of variable i is limited inside the loop only. As it is defined inside the loop. As the loop execution completes the scope of variable i get finished.

Similarly, the scope of variable x is limited to only the main function execution. As the execution of main function completes, the lifecycle or scope of the variable finishes and variable gets destroyed as well.

So, the scope of local variable is limited to the block of code under which it is declared or defined whether it ls a function, a loop , a parameter or a block of code.

Global Variable

Global variables are defined outside the function and used to access from anywhere.

The scope of global variable is limited from the start of the program to the end of the program.

For ex :

class Sample
{
  int id=1;
  String name="toastguyz";
  test()
  {
    print("Sample "+id.toString());
  }
}
// global variable
int a=10;
void main()
{
	for(int i=0;i<2;i++)
        {
            print(i);
            print(a);
        }
  Sample S=new Sample();
  S.test();
}

Output :

0
10
1
10
Sample 1

In the above example, the scope of the global variable is from the beginning of the program to the end of the program. While the scope of the local variable i is limited to the end of the execution of the for a loop.

One other example of global variables are instance variables.

Instance variables are the variables declared inside the class but outside the method.

We have discussed about it prior. Checkout our tutorial on instance variable to get familiar with it.

Instance variable acts as global variable for the object of the particular class. The scope of the instance variable starts from the creation of the object and ends with the object destruction.

In the above example, instance variables id and name can be accessed globally for the object S in the class Sample.

So, a local variable is used whenever they need to manipulated inside a block of code only while global variables are used whenever the necessity to access the variable from anywhere in the program is required.

As local variables get destroyed as soon as the block of code gets executed while global variables will obtain the memory till the end of the program. So, both types of variables have their own specific purpose to use. So prefer to use a local variable in general wherever it is possible for memory efficiency. Because a single global variable requires a greater amount of memory in comparison to a local variable

It will be useful to understand the local and global variable usage to become a better developer.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mohamed
Mohamed
1 year ago

difference between instance and global?

Show Buttons
Hide Buttons