• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to design the two methods, simple case of deciding about how to pass arguments  RSS feed

 
Sriram Sharma
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Suppose I have two methods as given here below



Now the question is as here below
In the above two methods, I pass only one map argument.
Suppose that I donno how many arguments to pass to anotherMethod while coding and if that will be determined only during execution, how to design my "testMethod" and "anotherMethod"?

Regards,
Sriram
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you not know how many arguments to pass? A method has some purpose, so it should always know ahead of time what parameters it requires.

Can you explain what scenario you have in mind in which the amount of parameters can only be determined at runtime?
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sriram Sharma wrote:Hi All,

Suppose I have two methods as given here below



Now the question is as here below
In the above two methods, I pass only one map argument.
Suppose that I donno how many arguments to pass to anotherMethod while coding and if that will be determined only during execution, how to design my "testMethod" and "anotherMethod"?

Regards,
Sriram


May be you should take a look at var-args...
It works, if you are passing variable no of arguments of same types....

Eg: -

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sriram Sharma wrote:In the above two methods, I pass only one map argument.
Suppose that I donno how many arguments to pass to anotherMethod while coding and if that will be determined only during execution, how to design my "testMethod" and "anotherMethod"?

While I agree with Stephan's comments, R.Jain has pretty much covered it: Have a look at varargs, viz:A couple of other points:
1. ALWAYS supply types for your maps (unless you're still using Java 1.4, in which case update it; it is officially deceased). Eg:
Map<Integer, String> hm1 = new HashMap<Integer, String>();

2. Don't make assumptions about the type of Map you're using. You've correctly defined your HashMap as a Map, but all your names indicate that the map is, in fact, a HashMap. The whole point of using interfaces is that you can change the assignment later if you want; and
Map<Integer, String> hm1 = new TreeMap<Integer, String>();
is going to look rather odd.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!