Comparing an init() method with lots of parameters vs lots of seperate setters, or maybe a constructor.
I'm wondering whether there are any noticable performance or convention when we need to pass in more than 10 different parameters to an instance of a class?
by the way lots of these instance are created at the same time and I'm getting data from the database
In general I don't like initialising stuff in the constructor, I have been using seperate setters but I'm wondering whether the many method(setters) call will impact performance.
init() method looks cleaner with less code to type so I'm starting to change to this style, it also helps get rid of half the evil setters
Have you actually *seen* a performance issue? I can't imagine it'd make that much of a difference. Java's pretty darn fast. A huge wad of parameters would be faster than a huge wad of method calls (I'm assuming here!) but in terms of real-world performance... I'd have to benchmark it.
A method with ten parameters, however, wouldn't fly in most of my code reviews. Blindly avoiding setters seems just as bad, unless you're deliberately aiming for immutability (not a bad thing). I've sometimes taken to putting the properties/getters/setters in a base class to avoid the noise in a class that does actual work, but people make fun of me for that. Lombok might cure me of this; have to wait and see.
Depending on how much you need to ensure valid object state a more fluent, parallel group of setters might be appropriate, and can help alleviate the confusion caused by looking at a method with so many parameters. See http://c2.com/cgi/wiki?TooManyParameters.
My understanding tells me that each individual function call would bring a Stack and storing variables and Exception handling and all. So there ought to be a performance hit somewhere down the line? Its always 'Ok' if the requirements demand so and thats better left aside.
Ideally when creating objects I'd choose to go via the constructor approach if the number of parameters that decide on the same are considerable. Else I'd choose a setter method for this.
Though, like David, so rightly, pointed out the performance is hardly gonna be affected in each scenario, it again varies considering each scenario is different (at least slightly) from the other?