wood burning stoves 2.0*
The moose likes Java in General and the fly likes using data from Instance veriable is good or passing data as a method argument is good Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "using data from Instance veriable is good or passing data as a method argument is good" Watch "using data from Instance veriable is good or passing data as a method argument is good" New topic
Author

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

venkata vinay bysani
Greenhorn

Joined: Feb 09, 2010
Posts: 25
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

Joined: Oct 13, 2005
Posts: 38497
    
  23
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

Joined: Aug 16, 2005
Posts: 14116
    
  16

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.


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://aspose.com/file-tools
 
subject: using data from Instance veriable is good or passing data as a method argument is good