Java: Meaning of catch (final SomeException e)?

It basically means: Catch “SomeExceptionType” into the variable “e” with the promise that we won’t assign a different exception to “e” during the processing of the exception. Mostly this is overkill, as if I’m catching an exception into a temporary variable name (e only is valid for the exception handling block), I don’t have to … Read more

Anonymous-Inner classes showing incorrect modifier

Note that the wording in the JLS of that particular section has changed significantly since then. It now (JLS 11) reads: 15.9.5. Anonymous Class Declarations: An anonymous class is never final (§8.1.1.2). The fact that an anonymous class is not final is relevant in casting, in particular the narrowing reference conversion allowed for the cast … Read more

Final keyword in typescript?

It will be available since 1.4, you can check the Announcing TypeScript 1.4 article, “Let/Const” support section: “TypeScript now supports using ‘let’ and ‘const’ in addition to ‘var’. These currently require the ES6 output mode, but we’re are investigating relaxing this restriction in future versions.” Const should be implemented according to the article. You can … Read more

Declaring an ArrayList object as final for use in a constants file

You can easily make it public static final, but that won’t stop people from changing the contents. The best approach is to safely publish the “constant” by: wrapping it in an unmodifiable list using an instance block to populate it Resulting in one neat final declaration with initialization: public static final List<String> list = Collections.unmodifiableList( … Read more

How to handle a static final field initializer that throws checked exception

If you don’t like static blocks (some people don’t) then an alternative is to use a static method. IIRC, Josh Bloch recommended this (apparently not in Effective Java on quick inspection). public static final ObjectName OBJECT_NAME = createObjectName(“foo:type=bar”); private static ObjectName createObjectName(final String name) { try { return new ObjectName(name); } catch (final SomeException exc) … Read more

Must all properties of an immutable object be final?

The main difference between an immutable object (all properties final) and an effectively immutable object (properties aren’t final but can’t be changed) is safe publication. You can safely publish an immutable object in a multi threaded context without having to worry about adding synchronization, thanks to the guarantees provided by the Java Memory Model for … Read more

Compile-time constants and variables

Compile time constant must be: declared final primitive or String initialized within declaration initialized with constant expression So private final int x = getX(); is not constant. To the second question private int y = 10; is not constant (non-final in this case), so optimizer cannot be sure that the value would not change in … Read more