jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor Basics Doubt. K &B Pg131 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 "Constructor Basics Doubt. K &B Pg131 " Watch "Constructor Basics Doubt. K &B Pg131 " New topic
Author

Constructor Basics Doubt. K &B Pg131

sam augurs
Greenhorn

Joined: Jan 11, 2010
Posts: 3
Hi Folks,
According to K&B pg 131 topic Constructor Basics if the class doesn't have no-arg constructor then the following code fails to compile. For eg

public class Construct
{
Construct(int i)
{
System.out.println("Calling Constructor");
}

public static void main(String[] args)
{
Construct c = new Construct(); // won't compile, no matching constructor
}
}

Compiler error will be thrown at Construct c = new Construct();

I just modify the above code and passed var arg in constructor parameter like below code and again complied the below class which compiles and prints "Calling Constructor".

public class Construct
{
Construct(int... i)
{
System.out.println("Calling Constructor");
}

public static void main(String[] args)
{
Construct c = new Construct(); // No matching constructor but class compiles and generates output
}
}

Can anyone let me know why this is happening?
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
because var args means 0 or more arguments.


SCJP 1.6 96%
sam augurs
Greenhorn

Joined: Jan 11, 2010
Posts: 3
Thanks Neha, appreciate your quick reply which cleared my doubt
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
you are welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor Basics Doubt. K &B Pg131
 
Similar Threads
Private Constructor
doubt
Constructors!!!!Please help !!!!!
superclass no-arg constructor... mock question
inheritance