public class QuestionX {
private static QuestionX quest = new QuestionX();
private QuestionX(){
}
public static QuestionX getQuestion() {
return quest;
}
public void doSomething(){
System.out.println("In doSomething");
}
//other methods for this class to do things
}
How is this class used by a client? Provide code showing how you would use this class in the main method of a program and what constraints are placed on class usage by the above implementation? Explain how this has been achieved.
Client-Side Usage:
In order to use this class to instrantiate objects, we need to call the constructor, but the constructor is private, hence we cannot instantiate ojects directly, but we can use the static method getQuestion(). Then we can call the other methods on the object. For example:
Code in main():
QuestionX q = QuestionX.getQuestion();
q.doSomething();
Constraint:
You cannot instantiate an object of QuestionX by using the
new keyword as the
constructor has been privatized.
You can comment below the answer in case of any doubts and I will be happy to help.
Please give a thumbs up if the answer could be of help!
All the best!
Get Answers For Free
Most questions answered within 1 hours.