开发者

Why include the class name when referring to a static variable?

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 b开发者_JAVA百科elonged 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?


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


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.


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


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.


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!


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
    }
}


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.

Inside a Static method

  • Static variable can be accessed by
  1. In same class, either by variable name or class name.variable name.

  2. In different class, only by class name.variable name.

  • Instance variable can be accessed by
  1. In same class, by object of same class. (i.e) object.variable name.

  2. In different class, must be by object of relevant class. (i.e) object.variable name.

Inside a Instance method(Non static method)

  • Static variable can be accessed by
  1. In same class, either by variable name or class name.variable name.

  2. In different class, only by class name.variable name.

  • Instance variable can be accessed by
  1. In same class, only by variable name.

  2. In different class, must be by object of relevant class. (i.e) object.variable name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜