I was wondering if I can make my code clearer by indicating one variable is a static class variable. If it wasn't static I could use this.variableName, and everyone would look at it and immediately know that.
I know I could adopt a naming convention like s_variableName, but that seems a little odd to me and increases the learning curve of the code.
Is there an equivalent of "this" for static variables?
The "this" keyword is used as a reference to an instance. Since the static methods doesn't have (belong to) any instance you cannot use the "this" reference within a static method. If you still, try to do so a compile time error is generated.
Instance Variables: Instance variables are non-static variables and are declared in a class outside any method, constructor or block.
When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program.
In C, functions are global by default. The “static” keyword before a function name makes it static. For example, below function fun() is static.
Qualify it with the type name:
TypeName.staticVariableName
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With