This is a question to Kathy/Bert or any other person who might have read SCJP 5.0 book by Kathy and Bert.
On page number 129/130-->chapter 2-->Object Orientation-->Determine whether a Default Constructor will be created, it is written that in case there exists a constructor with arguments, then one can't have a no-arg constructor unless one types it in him/her self.
However, there is a mock exam quiz that looks like: public class Numbers..., negate, TreeSet etc. (sorry. can't quote the exact question because of copyright issues.The code is basically about negating the numbers added to a TreeSet.). There is no no-argument constructor typed, yet the following line of code:
compiles and runs fine.
I am confused why it is working, when it shouldn't have going by what is written in page numbers 129/130 of chapter 2.
Can you please reply as soon as possible ? my exam date is near. thanks in anticipation, Prashant
What happened when you executed the code yourself? When in doubt, try to code the example yourself. I have found a lot of mock exam questions to be incorrect. When I am not sure, I code the example myself to see what is correct :-)
update: I think i figured it out.... since the cstr calls for a var-args argument, namely int..., it's basically saying you can pass ZERO or more integers into the cstr... that's why this works!
============================================== Hi, I'm a little stumped by this one too.
According to SCJP 5.0 book by Kathy and Bert. ("the book") on page 129/130 (as mentioned):
If you want a no-arg cstr and you've typed any other cstr(s) into your class code, the compiler won't provide the no-arg cstr (or any other cstr) for you. In other words, if you've typed in a cstr with arguments, you won't have a no-arg cstr unless you typed it in yourself!
That's pretty clear if you ask me. Now on the mock exam the following code appears (it compiles just fine and runs) but why??