0

What's a static variable?

is there a difference between static and local variable?

23rd Feb 2017, 4:53 AM
Gokul Raj
Gokul Raj - avatar
4 Réponses
+ 2
Example: static int y = 0; When a variable is declared with the keyword static , its called a class variable . All instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to create a instance.
23rd Feb 2017, 5:01 AM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
+ 2
Yes Local variable:Variables under the block of function is called local variable and get destroyed after the function is completed Static variable:If you declare any variable as static, it is known static variable.It saves your memory.Suppose a class called Students Suppose there is a class called students class Student{ int rollno; String name; String college="ITS"; } There are 500 students in my college, now all instance data members will get memory each time when object is created.All student have its unique rollno and name so instance data member is good.Here, college refers to the common property of all objects.If we make it static,this field will get memory only once AND Java static property is shared to all objects or is same for every object.
23rd Feb 2017, 5:10 AM
Fahad
Fahad - avatar
+ 1
A static (accessed throughout whole class) variable last the whole program and can be accessed by other functions within your program while local can only be accessed in the function/method you created it in.
23rd Feb 2017, 5:05 AM
Isley
Isley - avatar
0
Thank you for all!
23rd Feb 2017, 7:35 AM
Gokul Raj
Gokul Raj - avatar