Cannot reference “X” before supertype constructor has been called, where x is a final variable

The reason why the code would not initially compile is because defaultValue is an instance variable of the class Test, meaning that when an object of type Test is created, a unique instance of defaultValue is also created and attached to that particular object. Because of this, it is not possible to reference defaultValue in … Read more

Scala – initialization order of vals

Vals are initialized in the order they are declared (well, precisely, non-lazy vals are), so properties is getting initialized before loadedProps. Or in other words, loadedProps is still null when propertiesis getting initialized. The simplest solution here is to define loadedProps before properties: class Config { private val loadedProps = { val p = new … Read more

Make private methods final?

Adding final to methods does not improve performance with Sun HotSpot. Where final could be added, HotSpot will notice that the method is never overridden and so treat it the same. In Java private methods are non-virtual. You can’t override them, even using nested classes where they may be accessible to subclasses. For instance methods … Read more

How is concatenation of final strings done in Java?

http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.28 says that Simple names (§6.5.6.1) that refer to constant variables (§4.12.4) are constant expressions. http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.28 also says: A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following: Literals of primitive type and literals of type String (§3.10.1, … Read more

How to extend a final class in Java

You can’t, basically. It looks like the developer who designed Bar intended for it not to be extended – so you can’t extend it. You’ll need to look for a different design – possibly one which doesn’t use inheritance so much… assuming you can’t change the existing code, of course. (It’s hard to give more … Read more