diferente between static int and int | SoloLearn: Learn to code for FREE!


diferente between static int and int

can someone tell me what diferente between static int and int, thx


12/6/2019 9:28:35 AM


4 Answers

New Answer


static have different meaning depending on where it is used. static is basically a storage class specifier. A static variable is allocated memory in a different part(section) of the program. Static variables are initialized only once in your program and in the order they are listed. They are destroyed in the reverse order of creation after the main ends. As a local variable in a function static variables retain their last value. It is useful in recursive functions or when you want to call a function multiple times and yet retain some previous calculated value so that you can use that value again. A local variable int (without static) holds it's value for the duration of the function call or a block scope in which it is declared. Once the function returns or the block scope ends, the variable is destroyed (because it is allocated on function stack) and is undefined for the rest of the code. static variables are always initialized to their default type value if not initialized explicitly. static variable in the global scope defines their linkage (visability). They have internal linkage i.e they are not visible outside of the file or translation unit they are defined in. A global int has an external linkage i.e they are visible outside of the file or translation unit they are defined in.


That's the best explanation ~ swim ~ . i'm also confused and always think of why there's "static" in OOP


Thank you Mirielle🐶 [Inactive] 🙏 ☺ 👍 In OOPS static has yet another use - accessing the member variables and methods without creating objects like you know Python static methods.


A static variable is common to all the objects of the class. Because the static variable is associated with a class. When a static variable is loaded in memory (static pool) it creates only a single copy of static variable and shared among all the objects of the class. Memory allocation of variables only happens once when the class is loaded in the memory. If you made any changes in the value of a variable by any object, then it will be reflected in other objects also. For more details https://javagoal.com/how-static-variable-works/