Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class ClassicSingleton{private static ClassicSingleton instance = null; p

ID: 3830277 • Letter: P

Question

public class ClassicSingleton{private static ClassicSingleton instance = null; protected ClassicSingleton(){} public static ClassicSingleton getlnstance(){} if(instance==null) {instance = new ClassicSingleton();} return instance;}} Consider the Java code true false - the Singleton object can be accessed even though it is private true false - instance = new ClassicSingleton(). will not work because the constructor is protected true false - A class can extend true false - the Singleton object will be loaded will be created when the class is loaded

Explanation / Answer

public class ClassicSingleton
{
    private static ClassicSingleton instance = null;
    protected ClassicSingleton() {}
    public static ClassicSingleton getInstance()
    {
       if(instance == null)
           instance = new ClassicSingleton();
       return instance;  
    }
}

The Singleton object can be accessed even though it is private. True. It can be accessed by instances of the class.
instance = new ClassicSingleton() will not work because the constructor is protected. False. A protected constructor can be called from within the class.
A class can extend.   True. It can be extended.
The Singleton object will be loaded will be created when the class is loaded. False.