• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor Basics Doubt. K &B Pg131

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because var args means 0 or more arguments.
 
sam augurs
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Neha, appreciate your quick reply which cleared my doubt
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic