File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Calling a Hashtable from another class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling a Hashtable from another class." Watch "Calling a Hashtable from another class." New topic
Author

Calling a Hashtable from another class.

Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
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

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
Jerri Loh
Ranch Hand

Joined: Jul 06, 2010
Posts: 31
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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Jul 06, 2010
Posts: 31
Thanks! I'll work on straight awaaaayyy.!
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
have fun ;)
 
jQuery in Action, 2nd edition
 
subject: Calling a Hashtable from another class.