File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is It good to make function call in Constructor?

 
Abhijit Das
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Is it a good practice to make some function calls in side constructors?



thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the method is private, sure. That way you can split separate parts of code into methods, so you're constructor does not become one large piece of code.

If the method is not private (i.e. it can be overridden) you need to be a bit more cautious, as any sub class can incorrectly override it leading to unexpected errors.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if the method is private, there are things you have to watch out for. As long as you're in the constructor (or in a method called by the constructor), you must keep in mind that the object isn't fully initialized yet. The method must be aware of that, and not assume that the object is fully initialized.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic