What happens when a variable is declared as static? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What happens when a variable is declared as static?

5th Sep 2016, 2:22 PM
Ved
Ved - avatar
4 Answers
+ 1
When you declare a variable as static, it means that you dont need to make an instance of the class in order to access it rather it belongs to the class itself and can be accessed via the class name. Note that since this is static, it means that the variable is shared by all instances of that particular class.
5th Sep 2016, 2:34 PM
Ousmane Diaw
+ 1
that means it can directly access from memory with its class name.and no need to make instanse of class. ex: class A{ static void add()//method } // so it can call directly like : A.add();
6th Sep 2016, 1:25 PM
adarsh
0
static is a storage class ,used for memory management . if any method or variable is declared static than it means that the scope of that method or variable is with that class in which they are defined static. they get cleared from the memory asa class is over. also there is no need to create instance of that class to call that method we may call it directly with the class name. And we also have static block that is used to initialize static variable ,also we may define a class as static but it should be an inner class. we can only use static variable in a static method otherwise we can use local variable if don't want to make a static variable
7th Sep 2016, 12:01 PM
Ankur Sharma
Ankur Sharma - avatar
- 1
Static is then associated with the variable.
5th Sep 2016, 2:32 PM
Kevin McMinn
Kevin McMinn - avatar