1

I have a class that basically parses a file and stores results in a hashmap , this class should handle all files in a directory (usually <10 files) . The process is linear with a single thread.

for the sake of best practice, should it be a static or dynamic class ?

4

4 に答える 4

3

If calling your parsing method makes sense even when no 'object' has been created and initialized, use a static method

ie: do not use

Parser p = new Parser();
HashMap result = parser.parse("directory");

When you can easily do the following:

HashMap result = Parser.parse("directory");

Java: when to use static methods

于 2012-08-08T20:29:59.830 に答える
1

I believe that the static modifier is appropriate if you don't have any reason to instantiate the class.

Also, according to this question only nested classes can be called static, and when they are, you can use the nested class without making an instance of the outer class.

于 2012-08-08T20:24:07.323 に答える
0

I am not sure about the class, but you could make this hashmap static with public access (directly or via accessors).

于 2012-08-08T20:22:04.657 に答える
0

Dynamic class loading is done when the name of the class is not known at compile time. I don't see reasons to use 'dynamic' modifier in your particular case.

于 2012-08-08T20:25:34.097 に答える