Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is there nothing like a final keyword to disallow anyone changing static variables?

 
Monica Shiralkar
Ranch Hand
Posts: 842
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static variables can be accessed by only static members. But why is there nothing like a final keyword to disallow anyone changing static variables?
 
Paweł Baczyński
Bartender
Posts: 1781
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:Static variables can be accessed by only static members.

It's quite opposite. Non-static variables can be accessed only by non-static members.

Monica. Shiralkar wrote:But why is there nothing like a final keyword to disallow anyone changing static variables?

There is a final keyword in Java. You can declare a variable to be static final.
 
Chan Ag
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I think what is important to understand here is that for accessing static variables/methods, we don't require an instance cause they are associated with the class. Hence they can be accessed from a static as well as a non static context. I think the rest of the rules/opinions are just derivations around that fact. Because they are not associated with an instance, static members ( let's refer to variables and methods as members ) are often used to create utility methods.

And we can have final static members. They are constants that don't change value. For example a double PI should always be 3.142. So we'd create a double pi as a constant ( a static, final member ).

For accessing instance members however we require an instance obviously cause they are instance members. Instances have a state and instance members are associated with that particular state ( through that instance ).
And hence we cannot access them from a static context without first creating an instance.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Chan said, there is a final keyword for this. For primitives and Strings, it really does prevent you from changing the value. For other types, it prevents you from changing the reference, but not the value. For example, public static final List<String> doesn't prevent you from adding to the list. You'd need to make it an unmodifiable collection as well. Or honor the intent of the programmer in seeing the final.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica. Shiralkar wrote:But why is there nothing like a final keyword to disallow anyone changing static variables?

As everyone else has said, there is.

And I'll go one step further, and say that you should use it on every single static field you define - especially if they're public.
Why? Because, while public constants are not a bad thing, public variables almost invariably are.

Winston
 
Monica Shiralkar
Ranch Hand
Posts: 842
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks and sorry for the mistake
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic