• Post Reply Bookmark Topic Watch Topic
  • New Topic

is default constructor needed here?  RSS feed

 
vianyrajnish rajnish
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

There are some occasion in which the default constructor is not at all
needed.
Consider the case of java APi:
ex: class Color
here, Color c = new Color(3,45,200);
is meaningful.

but, Color c = new Color(); will give an compiler error.

My doubt is if we have a constructor with arguments, one should provide the default no argument constructor. but it contradicts above compiler error. please guide me.

thanks,
vinay rajnish
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should provide a no-argument constructor if, and only if, you want people to be able to create instances of your class without needing to pass any arguments. That's it. If there's no meaningful way to construct a default instance of your class, then don't provide a default constructor.

Now, there are some situations where having a default constructor is useful or necessary -- for example, a Servlet and an Applet both must have one, or they can't be created by their containers, and similarly for Java Beans. But for plain old Java objects, a default constructor isn't needed unless you want to provide one.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to nitpick a little bit on the terminology: the default constructor is the no-arg constructor that is automatically provided by the compiler when the source code doesn't contain an explicit constructor. So strictly speaking, you cannot write a default constructor, only an explicit no-arg constructor.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Quote by vianyrajnish rajnish:
My doubt is if we have a constructor with arguments, one should provide the default no argument constructor. but it contradicts above compiler error. please guide me.


I think his question is why there is a default constructor existing then in the built-in library?

Is that right rajnish?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavan Muthu:


I think his question is why there is a default constructor existing then in the built-in library?

Is that right rajnish?


No, that's not his question at all, which should be obvious given that he hasn't mentioned a single class that has a no-argument constructor in his post.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought he was asking that way. Thanks for pointing out that EFH.
 
vianyrajnish rajnish
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

Thanks for your replies ..... I think i have to be precise while asking questions because it created some confusion. And i keep in mind that point next time. Thanks for encouraging and for helping me out.


I cleared with my doubt now....

rule is :
if we have the constructor with arguments then compiler wont provide the no-argument constructor on its own , instead we have to provide the no - argument constructor( only if needed based on our requirements).


thanks,
Vinay rajnish
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!