0
What happens when a variable is declared as static?
4 odpowiedzi
+ 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.
+ 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();
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
- 1
Static is then associated with the variable.