What is the difference between global and local variable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is the difference between global and local variable?

Please give a answer..

19th May 2019, 3:38 PM
Vijendra Mewada
Vijendra Mewada - avatar
7 Answers
+ 3
A local variable is declared inside any function. Local variable are stored on the stack unless specified. A global variable is declared outside any function. Stored on a fixed location decided by a compiler.
20th May 2019, 6:59 AM
Ankit Mewada
Ankit Mewada - avatar
+ 4
Global variables are declared outside any function, and they can be accessed (used) on any function in the program.  Local variables are declared inside a function, and can be used only inside that function. It is possible to have local variables with the same name in different functions.
19th May 2019, 4:16 PM
Ashish Mewada
Ashish Mewada - avatar
+ 4
A local variable is destroyed when the control of the program exit out of the block in which local variable is declared. However, a global variable is destroyed when the entire program is terminated.
20th May 2019, 5:52 AM
Jitendra Mewada
Jitendra Mewada - avatar
19th May 2019, 3:53 PM
Da2
Da2 - avatar
+ 2
global variable is declared in outside of function it has file scope we can use it anywhere in program local variable is declared under the function and it has block scope
19th May 2019, 6:46 PM
Pankaj Wandre
Pankaj Wandre - avatar
+ 1
It's as simple as you spend one minute watching what's app message. The name itself reflecting global variable means available for inside as well as outside every scope i.e. for, while,if else etc. in the program.Whereas, local is limited to Personal scope i.e only in for, while etc. Outside use is forbidden. :-) like if you find interesting.
19th May 2019, 4:14 PM
Manan Sodagar
Manan Sodagar - avatar
+ 1
Java doesn't have traditional global variables, just static on classes, which are still technically not global
19th May 2019, 6:19 PM
HNNX 🐿
HNNX 🐿 - avatar