Public class ClassicSingleton {private static ClassicSingleton instance = null;
ID: 3830281 • Letter: P
Question
Public class ClassicSingleton {private static ClassicSingleton instance = null; protected ClassicSingleton() {} public static ClassicSingleton getInstance() {} if(instance == null) {instance = new ClassicSingleton();} return instance;}} true false-if the constructor was public, it would still follow the Singleton Pattern true false-if the constructor was private, it would still follow the Singleton Pattern true false-if protected classicSingleton() {} is not necessary since it does not do anything true false-if the instance was made public then many instance objects could be madeExplanation / Answer
Part A: if the constructor was public, it would still follow the Sigleton Pattern
Answer: True
public class ClassicSingleton{
private static ClassicSingleton instance = null;
public ClassicSingleton(){ }
public static ClassicSingleton getInstance(){
if(instance==null) {
instance = new ClassicSingleton();
}
return instance;
}
public static void main(String[] args) {
//Instance 1
ClassicSingleton instance1 = ClassicSingleton.getInstance();
//Instance 2
ClassicSingleton instance2 = ClassicSingleton.getInstance();
//now lets check the hash key.
System.out.println("Instance 1 hash:" + instance1.hashCode());
System.out.println("Instance 2 hash:" + instance2.hashCode());
}
}
Output:
Instance 1 hash:4072869
Instance 2 hash:4072869
Part 2: If the constructor was private, it would still follow the singleton Pattern.
Answer: True
public class ClassicSingleton{
private static ClassicSingleton instance = null;
private ClassicSingleton(){ }
public static ClassicSingleton getInstance(){
if(instance==null) {
instance = new ClassicSingleton();
}
return instance;
}
public static void main(String[] args) {
//Instance 1
ClassicSingleton instance1 = ClassicSingleton.getInstance();
//Instance 2
ClassicSingleton instance2 = ClassicSingleton.getInstance();
//now lets check the hash key.
System.out.println("Instance 1 hash:" + instance1.hashCode());
System.out.println("Instance 2 hash:" + instance2.hashCode());
}
}
Output:
Instance 1 hash:4072869
Instance 2 hash:4072869
Part 3: if protected classicSingleton(){} is not necessary since it does not do anything.
Answer: True
public class ClassicSingleton{
private static ClassicSingleton instance = null;
// private ClassicSingleton(){ } removing the constructor
public static ClassicSingleton getInstance(){
if(instance==null) {
instance = new ClassicSingleton();
}
return instance;
}
public static void main(String[] args) {
//Instance 1
ClassicSingleton instance1 = ClassicSingleton.getInstance();
//Instance 2
ClassicSingleton instance2 = ClassicSingleton.getInstance();
//now lets check the hash key.
System.out.println("Instance 1 hash:" + instance1.hashCode());
System.out.println("Instance 2 hash:" + instance2.hashCode());
}
}
Output:
Instance 1 hash:4072869
Instance 2 hash:4072869
Part 4: if the instancewas made public then many instance objects could bw made.
Answer: False
public class ClassicSingleton{
public static ClassicSingleton instance = null;
// private ClassicSingleton(){ } removing the constructor
public static ClassicSingleton getInstance(){
if(instance==null) {
instance = new ClassicSingleton();
}
return instance;
}
public static void main(String[] args) {
//Instance 1
ClassicSingleton instance1 = ClassicSingleton.getInstance();
//Instance 2
ClassicSingleton instance2 = ClassicSingleton.getInstance();
//now lets check the hash key.
System.out.println("Instance 1 hash:" + instance1.hashCode());
System.out.println("Instance 2 hash:" + instance2.hashCode());
}
}
output:
Instance 1 hash:4072869
Instance 2 hash:4072869
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.