Why Declare Variables as Local and Global

Fetching Comments ...

When we declare local Variables:

We declare local Variables when we want some variables for local use only and which we don’t want later. If we need Variables only inside 1 method then don’t declare them as Global Variables. Local Variables store value for less time, I mean to say that when execution of method finish then local Variables lose their value and become available for another method. Using Local Variables manage memory allocation in JVM. That’s why avoid declaring Variables as Global. Declare Variables as Global when its requested in multiple methods.

When we Declare Global Variables:

We declare Global Variables when we need same value inside multiple methods and inner classes. Global Variables work as communication medium between multiple methods.

For example, if we want to transfer value from 1 method to another method then we can pass value to Global Variables and get value from that Global Variables inside another method.

Share This:

LearnPainLess On Instagram

Get The Best Of All Hands Delivered To Your Inbox

Subscribe to our newsletter and stay updated.