Timeline for answer to Change private static final field using Java reflection by thecoop
Current License: CC BY-SA 2.5
Post Revisions
13 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 18, 2019 at 21:09 | comment | added | Some Name |
No, it does not break the JVM. JVM allows static final fields to be modified by methods of the same class.
|
|
| May 19, 2016 at 16:00 | comment | added | crush | @Martin I know that your comment is old, and perhaps your views have changed by now, but I thought I'd just add this: Singletons are evil for reasons that have nothing to do with Java. They add hidden complexity to your code. Additionally, they can make it impossible to unit test without also knowing that n singletons must also be configured first. They are the antithesis of dependency injection. Your team might make the decision that the pitfalls of having hidden complexity don't outweigh the convenience of Singletons, but many teams take the opposite stance for good reason. | |
| Jan 13, 2015 at 14:28 | comment | added | naXa | @Vincent, isn't it contradict to the @Péter Török's comment? | |
| Dec 11, 2012 at 23:03 | comment | added | Vincent | The final modifier is not intended to protect a field from change. It just indicates, that the programmer of the class doesn't recommend changing this field. Same with accessors. They are only intended to show a user of a class what methods/fields are safe to use. | |
| Jun 11, 2011 at 14:54 | comment | added | Martin | @Tom: I have 20 years of experience in Software development. I learned a dozen and more programming languages. I researched the details about Singletons in Java. So what make you think your are right? | |
| Jun 10, 2011 at 12:16 | comment | added | Martin | @Tom: Did you ever read up why singletons are evil? I did! Now I know that they evil only in Java. And only because of the availabilty of a user defined class loader. And ever since I know all this and I don´t use user defined class loader I use singletons without regret. And so does Scala where singleton are a first class language feature — That singletons are evil is is a well known false myth. | |
| Jul 22, 2010 at 17:39 | comment | added | Tom Hawtin - tackline | @thecoop It is well known that singletons (a.k.a. globaltons) are evil. | |
| Jul 22, 2010 at 16:49 | comment | added | thecoop |
@Tom what about a Singleton using a getInstance() method that assigns the instance to a static field? You'll also need to reassign a mutable static field when it's a WeakReference<T> and the original reference target has disappeared.
|
|
| Jul 22, 2010 at 14:16 | comment | added | Tom Hawtin - tackline | @bcat It's almost always wrong. You might get away with it for a cache (or you might not). | |
| Jul 22, 2010 at 12:37 | comment | added | bcat | @Tom: In general that's probably true, but I wouldn't outlaw all static mutable variables. | |
| Jul 21, 2010 at 19:34 | comment | added | Tom Hawtin - tackline |
And a field not declared final should not be declared static.
|
|
| Jul 21, 2010 at 16:47 | comment | added | Péter Török |
Moreover, final has a special role in multithreaded execution - changing final values would break the Java memory model too.
|
|
| Jul 21, 2010 at 16:38 | history | answered | thecoop | CC BY-SA 2.5 |