This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Is It good to make function call in Constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Is It good to make function call in Constructor?" Watch "Is It good to make function call in Constructor?" New topic
Author

Is It good to make function call in Constructor?

Abhijit Das
Ranch Hand

Joined: Sep 25, 2007
Posts: 156
hi,
Is it a good practice to make some function calls in side constructors?



thanks


Abhijit Das
SCJP 5.0 | SCWCD 1.5
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Is It good to make function call in Constructor?
 
Similar Threads
How to draw US map ?
Recursive HashMaps?
Query regarding wrapper classes
Fetch multiple keys at one go