Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a Hashtable from another class.

 
Jerri Loh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.

I am currently trying programme my code(P) to lookup a hashtable from another class(F).
IS that possible?
Given that the hashtable is in the main method of (F).

Should I apply a toString() in (F) and make it a String variable before I call it into(P)?

If I do that, how do I reassimilate the (Key, value) format in (P).
There is always a case of "incompatible types" in the process. Any advice?

Thanks a million.

Regards, Jemima
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhm, what do you mean by 'putting it into the main method'.

If you want to access data from another class, you need to make your HashMap an instance variable and provide a getter for it.
 
Jerri Loh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhm, what do you mean by 'putting it into the main method'.
Sorry, I don't get you. I didn't say that.

But thanks so much for yr reccomendation of using the HashMap, I will study that now. However, don mind me asking, when you say
provide a getter for it

you mean I need to develop a get() for it, do i understand you correctly?

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that the hashtable is in the main method of (F).


This is what you said ;).

Hashtable is an implementation of Map that isn't in use any longer (the class name doesn't even follow java code conventions ;) ). So, use HashMap instead.

Everything that you declare inside a method will remain inside the method. If you want to make it part of the object's state, that you need to make those variables instance variables.

For the sake of encapsulation, you don't make your HashMap instance variable public, but rather keep it private and provide a method (a getter) that returns the Map to the client.


Just as an aside, you generally would not want to return a HashMap for manipulation purposes to the client, but for practice purposes it's ok I guess.
 
Jerri Loh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I'll work on straight awaaaayyy.!
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have fun ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic