Mark Kevin wrote:That's not correct. The first bullet on page 152 proves it. "A constructor is always invoked when a new object is created." You're splitting non-existent hairs. When you create an object you invoke a constructor in these circumstances; they are inseparable.
Although that's a good attempt, I don't think it fits well for a bullet list notation where statements are mostly concise and to the point.
"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."
Mark Kevin wrote:False logic. Incomplete is not necessarily incorrect.
Mark Kevin wrote:That would be like saying a person is from Brussels, but because you didn't mention he was also from Belgium he's not from Belgium.
The suggestion you made was incomplete and thus incorrect.
Mark Kevin wrote:(I made a mistake. Let me rephrase that).
False logic. Incomplete is not necessarily incorrect. That would be like saying a person is from Brussels, but because you didn't mention he was also from Belgium he's not from Brussels.
It depends on the interpretation of the word "invoke".
But I also know that if you want to invoke a constructor like you would invoke a method (e.g. to pass some default parameters to an overloaded or parent constructor), you can do this only from within another constructor.
it's likely people will complain about that statement being incorrect because it states you can only invoke a constructor from within a method with the call to the constructor preceded by the keyword new.
Mark Kevin wrote:Where did you get that from? It offers additional ("or") alternatives to constructor invocations. If you want to expand those other options, well, those can be added as well. You are working against your own argument because the bullet is narrowly stated ("the only way").
Mark Kevin wrote:According to your book
Mark Kevin wrote:Here's an idea. Maybe it would be clearer if it said, "Where the call to the constructor is not preceded by the keyword 'new', the only way a constructor can be invoked is from within another constructor using this() or super() as the first statement."
What do you think?
There's no suggestion at all in that statement (because it ends with a dot) that "from within a method" is not the only thing you can do if you call the constructor preceded by the new operator. So either call constructor from within another constructor (using this/super) or call constructor preced by new from within a method. These are the only 2 options I can see (based on that statement).
Definitely better than the previous one (as it's more concise and still correct) But half of the statement (or even slightly more) is almost a copy of the second bullet point of that page (about each constructor having this() or super() as the first statement). And also wondering if the accompanying code snippet should not be changed as well.
Like I said in one of the other posts: based on that code snippet, I think the authors just want to emphasize you can't invoke the constructor by just using its name (like you would invoke a method). And thus that first sentence should be replaced with something like "Trying to invoke a constructor like you would invoke a method (using only its name) is not allowed". The part about this() or super() is already mentioned in the 2nd bullet point.
Mark Kevin wrote:You continue proving my point and ignoring the bullet which states the "only" way a constructor can be invoked. Now you agree with me. It's not the only way.
Mark Kevin wrote:In fact, I've been getting second opinions by some, apparently, knowledgeable Java programmers who agree that the bulleted statement is false.
Mark Kevin wrote:Instead of arguing about a point we agree on, since you now agree that the bullet is false as written, we should try to fix it so others aren't mislead as I was.
Roel De Nijs wrote:...but you can not call the constructor directly. If you want to call a constructor directly, it can only be done from within another constructor.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:I know that from the context the implication that "invoked" is used to mean "call directly" is more plausible but that does not help a new programmer.
If you want to call a constructor directly, it can only be done from within another constructor.
So those knowledgeable Java programmers seem to be an SO question. Always BeForthrightWhenCrossPostingToOtherSites. Thanks!
Mark Kevin wrote:Calling B() invokes this(), but what does this() invoke? It invokes B() which is not "another" constructor. Invoking B() with B bObject = new B(); executes this() which invokes the same no-argument constructor, B(). It's not another constructor but the same constructor, twice. The word "another" also needs to be removed and just call it "a constructor." The word "another" implies two different constructors.
Mark Kevin wrote:Bullet 1) "A constructor can be invoked directly from within a constructor using this() or super()."
Mark Kevin wrote:Bullet 2) "You cannot call a constructor directly from within a method."
(Assuming it's clear that the word directly means "without preceding the call to the constructor with 'new'".)
Mark Kevin wrote:I'm not offended that you are accusing me of being dishonest. I'm the one who posted the question to get other opinions. There's nothing wrong with that.