aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes var-args / no-args constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "var-args / no-args constructor" Watch "var-args / no-args constructor" New topic
Author

var-args / no-args constructor

Christian Kindler
Greenhorn

Joined: Apr 16, 2009
Posts: 18
The following code is from Bates, Bert and Kathy Sierra. "SCJP SunĀ® Certified Programmer for JavaTM 6 Study Guide" (Master Exam):



Because there is no no-arg (default) constructor I would assume that the compiler would complain something like this:

Numbers.java:13: cannot find symbol
symbol : constructor Numbers()
location: class Numbers
Numbers negatives = new Numbers();
^
1 error


But the code compiles without errors and the output is -3 -1 5! Apparently the var-arg constructor matches the call with no args. The same with methods: a method go(int... x) will be executed when calling go() and there is no method go() without parameters.

Maybe this is obvious to others (to me it was not), but it might be a good idea to to include a hint on that behaviour in exam preparation books...

I really start to hate var-args...
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
There are two ways that you can call a var-args method. One of them is by supplying a list of individual arguments (and this includes passing zero arguments.) The other is to pass an array reference of the same (or a compatible) type. When you use the first method and you pass no arguments, the var-args array in the method will contain 0 elements. Also, notice that you can't pass both individual elements and an array at the point of the call (only pass either individual elements, or an array, but not both.)


All code in my posts, unless a source is explicitly mentioned, is my own.
reji singh
Ranch Hand

Joined: Apr 06, 2009
Posts: 52
good question. nice answer Ruben.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: var-args / no-args constructor
 
Similar Threads
Question on Q32 of the exam prep
confused about a question in mock quiz SCJP book by KS & BB
TreeSet
Doubt: from SCJP6.0 K&B MockExam-1.
treeset question