Page 1 of 1

About Question enthuware.ocpjp.v7.2.1001 :

Posted: Wed Nov 18, 2015 2:02 pm
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.

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

Posted: Wed Nov 18, 2015 3:26 pm
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.

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

Posted: Wed Dec 09, 2015 3:42 pm
by fariz.siracli
So a single-element enum type is implemention of a singleton pattern ?
if it has more than 1 element what then ?

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

Posted: Wed Dec 09, 2015 8:36 pm
by admin
Then it is not a singleton!

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

Posted: Thu Dec 10, 2015 5:45 am
by fariz.siracli
yes understood. Thanks.

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

Posted: Fri Jan 06, 2017 8:33 am
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?

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

Posted: Fri Jan 06, 2017 8:54 am
by admin
The first check of if(theInstance == null) is not happening in the synchronized block.

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

Posted: Fri Jan 06, 2017 9:02 am
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.

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

Posted: Fri Jan 06, 2017 9:15 am
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.

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

Posted: Fri Jan 06, 2017 9:27 am
by jagoneye
Thank you will check it. :)