• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple doubt for you guys, about static class

 
Vinicius Souza
Ranch Hand
Posts: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Tony Docherty
Bartender
Posts: 2971
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 6114
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinicius Souza
Ranch Hand
Posts: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, I will try this Properties object and I got my code working using a static function to initialize my static Hashtable.
Flws!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic