Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 431
punkrockbuddyholly Avatar asked Jul 20 '11 08:07

punkrockbuddyholly


People also ask

Is static variable should be called with their class name?

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.

Why is a static field also referred to as a class variable?

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.

What is the purpose of making a class static?

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.

What is the purpose of static class in Java?

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.


4 Answers

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 image 37
T.J. Crowder Avatar answered Sep 21 '22 06:09

T.J. Crowder


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.

like image 43
Martijn Courteaux Avatar answered Sep 18 '22 06:09

Martijn Courteaux


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 image 153
Merlyn Morgan-Graham Avatar answered Sep 20 '22 06:09

Merlyn Morgan-Graham


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
like image 33
Leonard Brünings Avatar answered Sep 18 '22 06:09

Leonard Brünings