Question

public class QuestionX {     private static QuestionX quest = new QuestionX();      private QuestionX(){     ...

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.

Homework Answers

Answer #1

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!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private boolean isBalancedByNodeCount() { /**************************************************************************** Implement this method and replace the return statement below with your code. * Definition of Balance tree (On page 372 of book): * An unbalanced tree is created when most of the nodes are on one side of the root or the other. ****************************************************************************/    return false; }       public static void main(String args[]) { int[] values1 = {50,...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Q:Could you fill in the blank。 class Employee{ // field private String name; private (1) _float__...
Q:Could you fill in the blank。 class Employee{ // field private String name; private (1) _float__ salary; // constructor (2) ________________________________________ // method public (3) __String____ getName( ){ return name; } public float getSalary( ){ return Salary; } public (4) ________ compare( (5) _________________ { (6) ____________________________________ } }// class Employee public class EmployeeUI{ public static void main(String[ ] args){ Employee e1 = new Employee(“William”); Employee e2 = new Employee(“Rchard”); if(Employee.compare(e1, e2)) System.out.println(“same employee”); else System.out.println(“different employee”); } }//class EmployeeUI
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
public class Date {    private int month;    private int day;    private int year;...
public class Date {    private int month;    private int day;    private int year;    public Date( int monthValue, int dayValue, int yearValue )    {       month = monthValue;       day = dayValue;       year = yearValue;    }    public void setMonth( int monthValue )    {       month = monthValue;    }    public int getMonth()    {       return month;    }    public void setDay( int dayValue )    {       day =...
public class Programming { public static void main(String args[]) { System.out.println("I am learning how to program.");...
public class Programming { public static void main(String args[]) { System.out.println("I am learning how to program."); } } Compile this and execute this program then modify it to display I am learning how to program in Java. Change class name to Javaprogramming.java. compile and execute. Modify the Javaprogramming class so it prints two lines of output. Change name to Awesome. Add second output statement that displays That's awesome ! Save as awesome.java then compile and execute
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT