Why is a `val` inside an `object` not automatically final?

This is addressed explicitly in the specification, and they are automatically final: Members of final classes or objects are implicitly also final, so the final modifier is generally redundant for them, too. Note, however, that constant value definitions (§4.1) do require an explicit final modifier, even if they are defined in a final class or … Read more

What are the benefits of using identical String literals instead of a final variable?

They will be exactly the same. The literal is interned (any compile time constant expression that results in that string shares the same instance as all other constants/literals) in both cases and a smart compiler+runtime should have no trouble reducing both to the most optimized example. The advantage comes more in maintainability. If you want … Read more

Declaring a List field with the final keyword

No, the final keyword does not make the list, or its contents immutable. If you want an immutable List, you should use: List<Synapse> unmodifiableList = Collections.unmodifiableList(synapses); What the final keyword does is prevent you from assigning a new value to the ‘synapses’ variable. I.e., you cannot write: final List<Synapse> synapses = createList(); synapses = createNewList(); … Read more

Use of final local variables in java [duplicate]

Firstly, the part about variables being “overridden” – final has two very different meanings. For classes and methods, it’s about inheritance; for variables it’s about being read-only. There’s one important “feature” of final local variables: they can be used in local (typically anonymous) inner classes. Non-final local variables can’t be. That’s the primary use of … Read more

Checking for existence of C++ member function, possibly protected

I’ve put some thoughts on how to implement the things you requested and came to a totally different conclusion. The problem at hand is very interesting: How do I check whether a class implements a hidden interface. Unfortunately the problem is a contradiction to the liskov substitution principle; one of the core object oriented principles. … Read more

Java : in what order are static final fields initialized?

Yes, they are initialized in the order in which they appear in the source. You can read all of the gory details in The Java Language Specification, §12.4.2. See step 9, which reads: … execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual … Read more

Quick Java question about private static final keywords for fields

I use Checkstyle with Eclipse, which results in a warning if the declaration is in a different order to the one you’ve specified, citing the Java Language Specification (JLS). For example, private final static String filename = “filename.txt”; results in ‘static’ modifier out of order with the JLS suggestions. They have this page which lists … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)