jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes VarArgs in constructor  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "VarArgs in constructor  " Watch "VarArgs in constructor  " New topic

VarArgs in constructor

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
We can have varargs in constructor, but in place of printng integer 2 , it prints some object.
i is being treated as object or wrapper.
Why .
Varargs is legal in constructor ,no error is coming up.???

class TestDemo1 {
TestDemo1(int... i) {
System.out.println("my cnstructor"+i);

publicvoid method(){}

public static void main(String args[]) {
TestDemo1 myclass = new TestDemo1(2);
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
There's nothing special about constructors here. You would get the same results if this were a regular method. The thing you have to remember is, i is an array. (A rather poorly-named array.) If you want the individual elements in the array, you need to do something that accesses those elements. For example:

"I'm not back." - Bill Harding, Twister
I agree. Here's the link: http://aspose.com/file-tools
subject: VarArgs in constructor
jQuery in Action, 3rd edition