Reputation: 9794
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?
Upvotes: 9
Views: 4821
Reputation: 487
A method can be static and non static. Similarly, inside a method, variables can be static and non static. Inside a it's own class and inside an other class also need to take into consideration. Let's see all the scenario now.
In same class, either by variable name
or class name.variable name
.
In different class, only by class name.variable name
.
In same class, by object of same class. (i.e) object.variable name
.
In different class, must be by object of relevant class. (i.e) object.variable name
.
In same class, either by variable name
or class name.variable name
.
In different class, only by class name.variable name
.
In same class, only by variable name
.
In different class, must be by object of relevant class.
(i.e) object.variable name
.
Upvotes: 1
Reputation: 8044
To clarify my comments:
import static java.math.BigDecimal.TEN;
...
public class Foo {
private static int COUNT = 0;
private static BigDecimal FIVE = new BigDecimal(5);
private BigDecimal height;
public void bar() {
height = null; // Good
this.height = null; // Unnecessary qualification
COUNT++; // Good
Foo.COUNT++; // Unnecessary qualification
height = TEN; // Fine
height = BigDecimal.TEN; // Fine
height = Foo.FIVE; // Fine; increases clarity by distinguishing it from similar imported static variables
height = FIVE; // Fine; perhaps the additional clarification is unnecessary
}
}
Upvotes: 1
Reputation: 324
Apart from the reviewer point of view (even you in the future :-) ), it is also good to keep the class name prefix for a quick refactoring when needed. It will spare you the changes all over your source code. Consider that too!
Upvotes: 1
Reputation: 1075467
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.
Upvotes: 4
Reputation: 68907
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.
Upvotes: 1
Reputation: 13272
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
Upvotes: 3
Reputation: 59151
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
Upvotes: 8