About Question enthuware.ocpjp.v7.2.1001 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Etruskas
Posts: 2
Joined: Wed Nov 18, 2015 1:34 pm
Contact:

About Question enthuware.ocpjp.v7.2.1001 :

Post by Etruskas »

Hello,
Question enthuware.ocpjp.v7.2.1001 :
Which of the following options is a valid implementation for singleton class SpeedSensor?

One of the options is this. Marked as correct:

Code: Select all

class SpeedSensor{
  private static class SpeedSensorHolder {
    public static SpeedSensor ss = new SpeedSensor();
  }
  public static SpeedSensor getSpeedSensor() {
    return ss;
  }
}
Explanation says: „This is also a valid singleton implementation.“

How this can be a valid singleton implementation, and a correct option?
1. It doesn't compile. Compilation error „ss cannot be resolved to a variable“
2. Class constructor is package private.


Other option

Code: Select all

public class SpeedSensor {
  private SpeedSensor theInstance = null;
  private SpeedSensor() {
  }
  public SpeedSensor getInstance() {
    if (theInstance != null) {
      theInstance = new SpeedSensor();
    }
    return theInstance;
  }
}
This option also has problems, but this is marked as incorrect option.
Explanation says: This is one of the oldest commonly used but incorrect technique to implement a lazily loaded singleton class. However, it does not ensure that only a single instance is created when used in a multi threaded environment.

How this can be commonly used, if this option has problems:
1. There is no way to get a SpeedSensor. getInstance() is not static
2. getInstance() check if (theInstance != null). So it never initializes theInstance field, and always returns null.

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

Re: About Question enthuware.ocpjp.v7.2.1001 :

Post by admin »

You are right. This has now been fixed.
Regarding option 5, this implementation was quite popular earlier but was later discredited because of the reason mentioned. That is why it is an incorrect option.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

fariz.siracli
Posts: 22
Joined: Mon Jul 06, 2015 11:45 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1001 :

Post by fariz.siracli »

So a single-element enum type is implemention of a singleton pattern ?
if it has more than 1 element what then ?

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

Re: About Question enthuware.ocpjp.v7.2.1001 :

Post by admin »

Then it is not a singleton!
If you like our products and services, please help us by posting your review here.

fariz.siracli
Posts: 22
Joined: Mon Jul 06, 2015 11:45 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1001 :

Post by fariz.siracli »

yes understood. Thanks.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1001 :

Post by jagoneye »

This is one of the oldest commonly used but incorrect technique to implement a lazily loaded singleton class. However, it does not ensure that only a single instance is created when used in a multi threaded environment.
For it to work correctly, theInstance field needs to be declared volatile.
I don't understand the explaination since you are synchronizing the class,
then only a single thread can use static methods or members at the same time.
So how will it allow to create more than one instance in multithreaded environment?

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

Re: About Question enthuware.ocpjp.v7.2.1001 :

Post by admin »

The first check of if(theInstance == null) is not happening in the synchronized block.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1001 :

Post by jagoneye »

admin wrote:The first check of if(theInstance == null) is not happening in the synchronized block.
Still didn't get you. :( Even so the instance is static and that means only one object will exist. Hope you can elaborate with a code and trace it.

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

Re: About Question enthuware.ocpjp.v7.2.1001 :

Post by admin »

Explained in detail here: http://javarevisited.blogspot.in/2014/0 ... -java.html
There are several articles on this issue. Just google.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1001 :

Post by jagoneye »

Thank you will check it. :)

Post Reply

Who is online

Users browsing this forum: No registered users and 110 guests