Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,849 members, 7,817,517 topics. Date: Saturday, 04 May 2024 at 01:36 PM

What The Difference In Local, Global, Static Variable Declearation - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / What The Difference In Local, Global, Static Variable Declearation (849 Views)

Variable In Python FREE Tutorial / How Is Iot Creating A Difference In The Development Of Mobile Apps? / Java Programmers - How Do I Pass Variable Value From A Classa To Another Classb (2) (3) (4)

(1) (Reply) (Go Down)

What The Difference In Local, Global, Static Variable Declearation by Mrchusenvessel(m): 12:35pm On Mar 26, 2017
help me explain please
Re: What The Difference In Local, Global, Static Variable Declearation by CodeBlooded: 2:18pm On Mar 26, 2017
To my understanding:
Local variable is a variable defined in a method/function, whose scope is limited to that method/function.

Global variable is a variable that is defined in a class, outside any method/function. Its scope span across all the method of the class and any other class that extends that class.

Static variable is a variable that can be called without referencing it with an object of the class. If it is to be called in another classB that inherits from classA then className.variableName is used as reference.

So the declaration.....
Local variable is declared in a method/function.
E.g public void decl(){int x;}

Global variable is declared in a class but outside any function. Not even inside main function.
E.g int x;

Static variable is the same as global variable when it comes to declaration except that the keyword static will be added when declaring it.
E.g static int x;

1 Like

Re: What The Difference In Local, Global, Static Variable Declearation by pseudonomer: 3:46pm On Mar 26, 2017
Global is declared outside the method/function. While Local is declared inside the function/method. Static variable gets memory only once in the class at the time the is class loading...

Meaning the global variable can be accessed anywhere within the class, local variable can be accessed only within the method and meaning not accessible outside the function that it is being declared. And static variable can be accessed in static method.
Re: What The Difference In Local, Global, Static Variable Declearation by excanny: 5:16pm On Mar 26, 2017
A local variable is declared within a block of code and not visible outside this block.

Example:

function Sum()
# ―――block of code starts here
var a = 3;
var b = 5;
var sum = a + b;――used locally only

Print(sum);

#―――block of code ends here

A global variable can used in different blocks of codes that are within the class. Usually declared on top of all the blocks that can see it.

Example:

class MyClass
#
var a = 3;
var b = 5;

function Add()
# ―――block of code starts here

var sum = a + b;――assessed globally

Print(sum);

#―――block of code ends here

function Subtract()
# ―――block of code starts here

var diff = a - b;――accessed globally

Print(diff);

#―――block of code ends here

#


A static variable is declared with the keyword static and can be accessed, without creating an instance of the class where it is declared.

Example:


class MyClass
#
static var a = 3;
var b = 5;
#

class Tester
#
MAIN PROGRAM
#
var staticVarUsed = MyClass % a;
Print(staticVarUsed);
Read;
#

#

(1) (Reply)

Linnworks Expert Needed Asap. / Html,css,bootstrap,jquerry,javascript,python/django,ccna,ccnp,cybersecurity.abj / If You An Appdeveloper And Based In Abuja, You're Urgently Needed.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 11
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.