| Author |
VarArgs in constructor
|
Lucky J Verma
Ranch Hand
Joined: Apr 11, 2007
Posts: 277
|
|
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
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
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
|
 |
 |
|
|
subject: VarArgs in constructor
|
|
|