File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using data from Instance veriable is good or passing data as a method argument is good

 
venkata vinay bysani
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic