Mark Kevin wrote:I understand it can't be called from within the method doStuff(), but what does "from within another constructor" mean? Is there an example of this somewhere? What kind of other constructor?
Mark Kevin wrote:It seems the sentence in the text would have been clearer and more accurate if it added, "or from within another method if you use the keyword 'new,' just like you said.
"The only way a constructor can be invoked is from within another constructor (using this or super as the first statement)."
Here is a bonus question: how can you make the Constructor class successfully compile with changing only one line?
Mark Kevin wrote:If I add 'new' to the call it compiles, but I would have to do that on two lines, not one.
Mark Kevin wrote:Any hints?
Mark Kevin wrote:That seems to make more sense. So what the book is saying is that every new constructed object has to go through this() or super().
Mark Kevin wrote:So let me get this straight. If I call a constructor from another method (new newConstructor(), and it invokes newConstructer() which has super() as its first statement, then, after the superclass is created the superclass somehow passes control back to the subclass and invokes the subclass constructor, the original constructor newConstructor().
Mark Kevin wrote:I think the part that threw me was "invoked." And, the words "from another constructor." If super() gets called, then something in the superclass must eventually invoke the constructor in the subclass. If this() is called, then it's almost recursive in a way and it calls this no-argument constructor, or one with arguments. So I understand the this() part of it which makes sense.
And I'll just assume that after super() runs, it invokes the subclass constructor, which it must, otherwise there's no "another constructor" invoking it. Sorry to be so dense, but I really have tried to put some thought into this.
Mark Kevin wrote:I suppose whether its super() or this(), either of these is an "invocation from within a constructor."
main() invokes new C() => C() constructor is invoked
Mark Kevin wrote:I still don't understand which "other" or "another" constructor invokes the subclass constructor, which is how the text is written.
Mark Kevin wrote:I also don't quite understand how your statement ...
main() invokes new C() => C() constructor is invoked
... is considered to be "invoking from another constructor" because it's from another method, not another constructor.
Roel De Nijs wrote:
C() explicitly invokes C(int) => C(int) constructor is invoked C(int) implicitly invokes super() => B() constructor is invoked B() implicitly invokes super() => A() constructor is invoked A() implicitly invokes super() => Object() constructor is invoked
Mark Kevin wrote:I'm searching for an invocation of the subclass constructor (in the super() scenario) from "another constructor" (which is why I erroneously thought it might come from the superclass). It begs the question: "Which is the 'another constructor' from which supposedly the subclass constructor is invoked?" That seems to be the nub of the issue.
Mark Kevin wrote:I have over 1,000 more flash cards to wade through.
Roel De Nijs wrote:
Mark Kevin wrote:Any hints?
Sure! Have a close look at the compiler error you get after compiling the Constructor class.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Mark Kevin wrote:All right, I think I solved your riddle. I changed the method name to Constructor() (I never would have thought of that), as both the constructor and method can have the same name, though I don't think that generally is a good idea.
Mark Kevin wrote:So it compiles (don't know why), but when I create an instance of Constructor and call the Constructor() method I get a slew of StackOverFlow errors because the method keeps calling itself. So just changing that one line, it seems, isn't effective from a practical point of view. I would have to do something else and change more than one line to make it run properly.
Mark Kevin wrote:And why won't it invoke the constructor?
Mark Kevin wrote:Now, using the following code for reference, from within which constructor ("another constructor") is the constructor public B() invoked? There is none. There is an invocation from within the main() method, but that's not a constructor. And it obviously can't be invoked from within public A(), which you explained.
Issue: From within which constructor is public B() invoked?
I think I have a valid point.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Roel De Nijs wrote:Ok, now I see what you meant, sometimes adding a code snippet can be crystal-clear
In this code snippet there's indeed no other constructor which will invoke B(). But that's not a violation of the bullet point in the study guide: "The only way a constructor can be invoked is from within another constructor". It says that you can only invoke a constructor from within another constructor. It doesn't say that every constructor must be invoked from within another constructor.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
The reason why the code compiles, I just have explained. If you still have doubts after this explanation, just let us know. And about the StackOverflowError you are absolutely correct because in your code snippet the method Constructor() simply invokes itself recursively. So if a method is recursively invoked without any exit condition, a StackOverflowError will be thrown at a given moment. The most easy fix is to change the method name (like in my code snippet, the name of the method invoking Constructor() is create). So this code snippet runs fine
Using "way" in the textbook, they clearly want to emphasis, that the only way a constructor can be invoked is only through another constructor
Issue: From within which constructor is public B() invoked?
Sachin Tripathi wrote:Using "way" in the textbook, they clearly want to emphasis, that the only way a constructor can be invoked is only through another constructor
What you are saying is about the number of constructor that can be invoked from another constructor, i. e. 1
Mark Kevin wrote:That bullet needs to be reworded because it is incorrect on its face and states that invoking a constructor from within main() is illegal, which it is not.
Mark Kevin wrote:So even though my change allowed it to compile I had it all wrong. I changed create() to Constructor(), but what I should have done is simply added a return type, void, to the constructor Constructor and turned it into a method. Then, using "new" I can create an object with Constructor c = new Constructor();
Mark Kevin wrote:Can you clarify something for me? Which line creates the object?
Mark Kevin wrote:So this line is now calling a method instead of a constructor? It creates an instance of the Constructor class by calling a regular method, the magic word being 'new'?
Mark Kevin wrote:
Issue: From within which constructor is public B() invoked?
What specific line of code invokes B()? From within which constructor ("another constructor")?
Not trying to be difficult. Trying to be accurate.
The latter one is done by invoking/calling the new keyword followed by a call to a constructor. And that's of course not the same as "invoking a constructor".
main() invokes new C() => C() constructor is invoked
In short: the constructor B() is not invoked from within another constructor.
Mark Kevin wrote:I believe, and I candidly admit I'm no Java expert, but I think the last bullet on page 129 should be reworded as follows (or something similar), and I think the authors would agree if they thought about it some more:
"The only way a constructor can be invoked is from within another constructor (using a call to super() or this()), or from within a method with the call to the constructor preceded by the keyword 'new'."
Roel De Nijs wrote:
Maybe that's some misinterpretation on your side or maybe not being clear at my side. But although you are correct (from each constructor exactly one other constructor is invoked), it's not what I intended to explain/clarify.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Roel De Nijs wrote:"The only way a constructor can be invoked is from within another constructor (using this() or super() as the first statement)."
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:
Roel De Nijs wrote:
Maybe that's some misinterpretation on your side or maybe not being clear at my side. But although you are correct (from each constructor exactly one other constructor is invoked), it's not what I intended to explain/clarify.
I added one more bullet point..![]()
Sachin Tripathi wrote:That surely improves the bullet point(make it more clear),and @Mark:No one argues with Roel here
He is a king,and this forum is his kings Landing(Believe me ,he never makes mistake)![]()
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
K&B7, page 129 wrote:The only way a constructor can be invoked is from within another constructor.
In other words, you can't write code that actually calls a constructor as follows:
Secondly your suggestion is also incorrect, because if you invoke the new operator followed by a call to the constructor, you can do it from anywhere (static and instance methods, static and instance initializer blocks and even constructors as well), it's not limited to methods only.
Trying to invoke a constructor like you would invoke a method (using only its name) is not allowed.
Don't forget there's a difference between invoking a constructor and creating objects. The first one can only be done from within another constructor (using this() or super()). The latter one is done by invoking/calling the new keyword followed by a call to a constructor (e.g. new ClassX();). And that's of course not the same as "invoking a constructor".
Mark Kevin wrote:My suggestion is not incorrect;
Mark Kevin wrote:"The only way a constructor can be invoked is from within another constructor (using a call to super() or this()), or from within static or instance methods, static or instance initializer blocks, or even constructors, if the call to the constructor is preceded by the keyword 'new'." Trying to invoke a constructor like you would invoke a method (using only its name) is not allowed."
The suggestion you made was incomplete and thus incorrect.
Don't get me started about those stupid light bulbs. |