While doing some Java homework I answered a question by writing an instance method, in the method I made use of some static final
variables that belonged to the class the method was in. I wrote the static variable names without prefixing them with the class' name, for example:
for(int i=0; i < MY_STATIC_VARIABLE; i++)
instead of
for(int i=0; i < MyClass.MY_STATIC_VARIABLE; i++)
This compliled and worked correctly. It was only later that I noticed I had forgotten to prefix the class' name. Does it matter whether I include the class name or not? Does a static final
variable act like a global variable within the context of its class?
Class variables also known as static variables are declared with the static keyword There would only be one copy of each class variable per class, regardless of how many objects are created from it. You can access a class variable without instantiation using the class name as className. variableName.
Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object.
The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.
In Java, the static keyword is primarily used for memory management. We can use the static keyword with variables, methods, blocks, and classes. Using the static class is a way of grouping classes together. It is also used to access the primitive member of the enclosing class through the object reference.
Does a
static final
variable act like a global variable within the context of its class?
Yes, all static
class-level members are accessible throughout the class's code (final
or otherwise), and they don't need to be prefixed with the class name. Including it or not is a style preference.
What's less obvious is that within an instance method, you could use this.MY_STATIC_VARIABLE
and the compiler would be perfectly happy, even though MY_STATIC_VARIABLE
isn't an instance field. (With public static fields, you can do that with any instance reference, not just this
.) You can do the same thing with static
methods. But it's horribly misleading to anyone reading the code. :-) Still technically a style preference, but I'd strongly recommend against it. Just mentioning it in case you end up reading code that looks like it must have a bug in it.
It compiles fine because of -- as you figured out -- the static final variable is in the same class. This means that the variable is in the "scope" of the code. And it simply doesn't matter if you include the prefix or not. Only when you want to access static varialbes from another class as the current, you have to say in which class the variable is located.
It is pretty much the same as saying this
or not. eg:
private String secret;
public String getSecret()
{
return this.secret;
}
Or
private String secret;
public String getSecret()
{
return secret;
}
This is exactly the same.
Does it matter whether I include the class name or not?
To your teacher, and future people reviewing code at companies you end up working for, maybe. But maybe not - If I were reviewing your code, I'd suggest leaving out the class name in this case.
To the compiler, no, it doesn't matter.
Does a static final variable act like a global variable within the context of it's class?
Sure does
Like Joey already said, if you are within the class you can access it unqualified. If you are however using it from another class then you should use the classname instead of a instance to access it, to make it clear that it is a static variable/constant.
MyClass instance = new MyClass();
instance.MY_STATIC_VARIABLE //not good
MyClass.MY_STATIC_VARIABLE //good
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