About Question enthuware.ocpjp.v8.2.1115 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
Martyjee
Posts: 32
Joined: Tue Oct 06, 2015 9:06 am
Contact:

About Question enthuware.ocpjp.v8.2.1115 :

Post by Martyjee »

I would like to add something to the explanation with respect to Java 8:
Therefore, synchronized keyword can be applied only to a non-abstract method or a block of code appearing in a method or static or instance initialization blocks.
Consider the following interface:

Code: Select all

public interface Synchronizable {
    static void sync() { 
        synchronized (Synchronizable.class) { //no problem!
        }
    }

    static synchronized void deSync() { //will not compile!
    }

    default synchronized void defaultSync() { //will not compile!
    }
}
For some reason, the synchronized keyword cannot be applied to static non-abstract interface methods, as well as non-abstract default methods. However, it is possible to synchronize those methods using a lock on the class (Synchronizable.class). That is exactly what we expect to happen when we put the synchronized keyword in the method's signature. However, compilation fails if we do that.

Finally,
All methods in an interface are implicitly abstract.
is not a valid statement anymore since Java 8!

Kind regards,

Martijn

admin
Site Admin
Posts: 10389
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1115 :

Post by admin »

Fixed.
thank you for your feedback!

Martyjee
Posts: 32
Joined: Tue Oct 06, 2015 9:06 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1115 :

Post by Martyjee »

I appreciate the fix.
However, to have a complete picture of the synchronized keyword w.r.t. Java 8, the explanation should be something like:

The synchronized keyword can be applied only to non-abstract methods that are defined in a class or a block of code appearing in a method or static or instance initialization blocks.

admin
Site Admin
Posts: 10389
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1115 :

Post by admin »

Done.
thanks again!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests