File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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
Wanderer
Sheriff

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
 
Don't get me started about those stupid light bulbs.
 
subject: VarArgs in constructor
 
Similar Threads
Confused about overloading
Dynamic Method Invocation
Variable Arguments
var-args
Method matching.