aspose file tools*
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
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: 541
Static variables can be accessed by only static members. But why is there nothing like a final keyword to disallow anyone changing static variables?
Pawel Pawlowicz
Ranch Hand

Joined: Apr 18, 2013
Posts: 596
    
  11

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
Bartender

Joined: Sep 06, 2012
Posts: 839
    
  14
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: 29259
    
140

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: 7064
    
  16

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?
Artlicles by Winston can be found here
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 541
thanks and sorry for the mistake
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why is there nothing like a final keyword to disallow anyone changing static variables?
 
Similar Threads
Instance Variables State
why inner class can't have static modifiers???
Why can't I make public variables in void methods?
static
private static final