This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes why is there nothing like a  final keyword to disallow anyone changing static variables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why is there nothing like a  final keyword to disallow anyone changing static variables?" Watch "why is there nothing like a  final keyword to disallow anyone changing static variables?" New topic
Author

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

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 639
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

Joined: Apr 18, 2013
Posts: 935
    
  14

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.


Formely Pawel Pawlowicz
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1013
    
  15
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30392
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7718
    
  20

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 639
thanks and sorry for the mistake
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why is there nothing like a final keyword to disallow anyone changing static variables?