Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Can a class have both public and private constructor?</p> </blockquote> <p>Yes, it is possible.</p> <blockquote> <p>A private constructor is needed to set the private field whose type is a private inner class. Is this encouraged or discouraged ? </p> </blockquote> <p>It depends on the situation. Whether you want other class to initialize the state of your object or not. Here I think you have created class CopyTree to return the Copy of Tree which is a private class. So TreeNode class would be encapsulated, hence it leaves you with option of using private constructor capture idiom.</p> <blockquote> <p>what is a better solution for a scenario listed below ?</p> </blockquote> <p>In my view private constructor capture idiom is the better solution.</p> <p><strong>For more information:</strong></p> <p>you could search for <a href="https://stackoverflow.com/a/11360712/1544069">private constructor capture idiom</a>.</p> <p>An example is given in Solution 53 of <a href="http://www.amazon.co.uk/JavaTM-Puzzlers-Pitfalls-Corner-ebook/dp/B001U5VJVS/" rel="nofollow noreferrer">Java Puzzlers</a>:</p> <p><strong>Puzzle 53: Do Your Thing</strong> </p> <blockquote> <p>Now it's your turn to write some code. Suppose that you have a library class called Thing whose sole constructor takes an int parameter:</p> </blockquote> <pre><code>public class Thing { public Thing(int i) { ... } ... } </code></pre> <blockquote> <p>A Thing instance provides no way to get the value of its constructor parameter. Because Thing is a library class, you have no access to its internals, and you can't modify it. Suppose that you want to write a subclass called MyThing, with a constructor that computes the parameter to the superclass constructor by invoking the method SomeOtherClass.func(). The value returned by this method changes unpredictably from call to call. Finally, suppose that you want to store the value that was passed to the superclass constructor in a final instance field of the subclass for future use. This is the code that you'd naturally write:</p> </blockquote> <pre><code>public class MyThing extends Thing { private final int arg; public MyThing() { super(arg = SomeOtherClass.func()); ... } ... } </code></pre> <p>Unfortunately, it isn't legal. If you try to compile it, you'll get an error message that looks something like this:</p> <pre><code>MyThing.java: can't reference arg before supertype constructor has been called super(arg = SomeOtherClass.func()); ^ </code></pre> <blockquote> <p>How can you rewrite MyThing to achieve the desired effect? The MyThing() constructor must be thread-safe: Multiple threads may invoke it concurrently.</p> </blockquote> <p><strong>Solution 53: Do Your Thing</strong></p> <blockquote> <p>You could try to stash the result of the invocation SomeOtherClass.func() in a static field prior to invoking the Thing constructor. This solution is workable but awkward. In order to achieve thread-safety, you must synchronize access to the stashed value, which requires unimaginable contortions. Some of these contortions can be avoided by using a thread-local static field (java.util.ThreadLocal), but a much better solution exists. The preferred solution is inherently thread-safe as well as elegant. It involves the use of second, private constructor in MyThing:</p> </blockquote> <pre><code>public class MyThing extends Thing { private final int arg; public MyThing() { this(SomeOtherClass.func()); } private MyThing(int i) { super(i); arg = i; } } </code></pre> <blockquote> <p>This solution uses an alternate constructor invocation. This feature allows one constructor in a class to chain to another constructor in the same class. In this case, MyThing() chains to private constructor MyThing(int), which performs the required instance initialization. Within the private constructor, the value of expression SomeOtherClass.func() has been captured in the parameter i and can be stored in the final field param after the superclass constructor returns.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload