• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

confused about a question in mock quiz SCJP book by KS & BB

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :-)
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prashant Bhard:
... There is no no-argument constructor typed, yet the following line of code:

compiles and runs fine...


Does the Number class have any constructor defined? If there are no constructors explicitly defined, then the compiler will automatically provide a default no-args constructor.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's okay, in general, to copy small bits of content like a mock question - we always recommend that you attribute the authors of the content - even if it's just a mock question, and then it's okay.

So, in this case I would go ahead and post the full question so that the discussion can be more meaningful.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

//nailed it.

==============================================
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??

Here is the code:


[ July 29, 2007: Message edited by: Noam Wolf ]
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this codes compiles fine because the constructor::

serves the purpose of both non-argumented constructor and argumented constructor...
but change your constructor to::

and your code will run into trouble !!
 
Prashant Bhardwaj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Naom and Priyam !.
I did not know that var-args accepts no arguments as well.
This was a googly!.
regards,
Prashant
 
a wee bit from the empire
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic