File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Simple doubt for you guys, about static 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 "Simple doubt for you guys, about static class" Watch "Simple doubt for you guys, about static class" New topic

Simple doubt for you guys, about static class

Vinicius Souza
Ranch Hand

Joined: May 18, 2011
Posts: 70

Hi guys, I'm creating a version of my microinterpreter in java and I need to know about some few things. The program in java is working nicely but because my lack of knowledge in java properly, I added the tokens on my tokenizer manually. I want to create a table where I can add new tokens easly without need to add it manually.
So, I created a util class where I putted??? some utility functions and where I putted?? my token table. See below:

See, so, how can I put this in a static table? I was thinking in Hashtable class but how can I add information to it statically? I dont want to instanciate the ScriptCommon class, I tryed this:

But this code doesnt compile...
Any sugestions ? Remember, I want it statically... a vector???
Thanks in advance!

Java is the best (I love C too) heehhh
Tony Docherty

Joined: Aug 07, 2007
Posts: 2838
You would probably be better off putting them in a properties file and then loading the properties file at runtime into a Properties Object. A Properties object extends Hashtable so you would be able to access the key values pairs in the same way you do now but the key value pairs would be in a text file so you can add new values without having to recompile your code.

If you really want to do it in a static class then use a static initializer to load the values into the Hashtable which BTW should really be a HashMap.
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4621

Why does it have to be static? Why are you hardcoding values? Why are you using legacy classes?

Just pass a Map<String, String> to your interpreter constructor. No static data, no legacy classes and you can read key-value pairs from a file to fill the map.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Vinicius Souza
Ranch Hand

Joined: May 18, 2011
Posts: 70

Thanks guys, I will try this Properties object and I got my code working using a static function to initialize my static Hashtable.
I agree. Here's the link:
subject: Simple doubt for you guys, about static class
It's not a secret anymore!