This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a Map which will be used by several methods of the class. Currently what i am doing is:
Passing the map as an constructor argument & intialize the Map which is there as instanse veriable of the class and later using it in the methods of the class.
I have doubt regarding the way i am doing is a good practice or passing the Map to each and every method several times as a method argument is a good practice?
Please advise which among these is a good practice to follow.
Don’t understand the question.
Which class do you want the Map in? Which constructor are you passing it to? Which class’ methods use that Map? What do you mean about passing the Map and then initialising it?
One point you haven’t considered: what if the Map is mutable (they usually are) and it is changed outwith your class and outwith your control?
There's no clear answer to this question that says "you must always prefer this instead of that". In some cases, one solution is appropriate, in other cases another solution.
The answer to this depends on what the class that you pass the Map into exactly represents, and what data the Map holds. Does it make sense for instances of the class to hold the Map? Or are you only passing it that way because it's convenient?
The question is about good OO design (not about performance or efficiency). Without knowing details of what your class is exactly and what the map is, it's hard to answer your question.