• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can a map value be a method?  RSS feed

 
Eric Matthew
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically I just want the keys to increment different int counter variables each time the keys are retrieved.

Example:



Not sure if I'm on the right track. A bit lost here. Thanks CodeRanch!
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow !! i thought below line should give compile time error but it dint.

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method is a class, so there's nothing to prevent a Method being used as a value in a Map. The syntax of a method call does not however constitute a reference to a Method. To learn more, go through the tutorial on Reflection -> Methods.

If HandType is used only in PokerDeck, I would make the enum private. I would also give it an int field count and increment and query the field, with no need for a HashMap.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that code actually compile?

  • 1: You appear to have statements lying free in the class: that is not permitted. Put them in the constructor.
  • 2: You are not putting a method into the Map. You are calling the method and putting its returned result into the Map. Since the method is void, there is no value to return and nothing to put into the Map. The compiler won't be happy.
  • You are nowhere putting a method into the Map. You are calling methods and putting their results into the Mao.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!