Is “public static final” redundant for a constant in a Java interface?

Variables declared in Interface are implicitly public static final. This is what JLS 9.3 says : Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields. Read through the JLS to get an idea why … 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

Why isn’t a qualified static final variable allowed in a static initialization block?

The JLS holds the answer (note the bold statement): Similarly, every blank final variable must be assigned at most once; it must be definitely unassigned when an assignment to it occurs. Such an assignment is defined to occur if and only if either the simple name of the variable (or, for a field, its simple … Read more

creating final variables inside a loop

Yes, it is allowed. The final keyword means that you can’t change the value of the variable within its scope. For your loop example, you can think of the variable going out of scope at the bottom of the loop, then coming back into scope with a new value at the top of the loop. … Read more

Modifying final fields in Java

Compile-time constants are inlined (at javac compile-time). See the JLS, in particular 15.28 defines a constant expression and 13.4.9 discusses binary compatibility or final fields and constants. If you make the field non-final or assign a non-compile time constant, the value is not inlined. For instance: private final String stringValue = null!=null?””: “42”;

Why private method can not be final as well?

Basically, it’s allowed because they didn’t feel like it’s worthwhile to put a special case prohibiting the private modifier. It’s like how you can also declare methods on an interface as public, or nested classes in an interface as static, even though those keywords are implied in interfaces. You can also declare final methods on … Read more

Why does the Java compiler not understand this variable is always initialized?

As part of aiming for portability, there is a very specific set of rules for what a compiler should accept and what it should reject. Those rules both permit and require only a limited form of flow analysis when determining whether a variable is definitely assigned at its use. See the Java Language Specification Chapter … Read more