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

1. If line c was deleted, is there a risk that code outside of class X could cre

ID: 3790128 • Letter: 1

Question

1. If line c was deleted, is there a risk that code outside of class X could create an improperly initialized instance of X?

2. Most Java compilers are not smart enough to figure out your answer to part a; as a consequence, they demand that you make a change to your program. What change?

3. If the keyword static was omitted on line d, how could code outside of class X create a new instance of X?

4. Assume that a program using the above code is completely debugged and that it works perfectly. Under these circumstances, would deleting the keyword private from line c but making no other changes have any effect on the correctness of the program?

class X private int i private X static x factory (int v) d X r new X() r. 1 return r other methods

Explanation / Answer

1. Yes, deleting line c will allow programmer to create object for class X any which way it wants using new operator. To avoid that we create a private Constructor and a method factory which helps to instantiate the object and provide the same to programmer.

3. Having a private Constructor for a class doesn't allow instantiating the class outside. Hence to achieve instantiating the object a static method providing the instance is added.

4. Removing line c didn't create any issues to the rest of the code