Take the 2-minute tour ×

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 ?

share|improve this question
What do you mean by "static or dynamic"? A class can be declared with the static modifier iff it's an static nested class. Please show code and attempt to explain what your question is again. –  oldrinb Aug 8 '12 at 20:21
Sounds like a pretty generic task, so I would go with static, but I am prepared to be overruled by much more knowledgeable Java geeks. :D –  brimborium Aug 8 '12 at 20:21
add comment

4 Answers

up vote 2 down vote accepted

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

share|improve this answer
add comment

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.

share|improve this answer
abstract is the keyword to use if there is no reason to instantiate the class. Static just means that you have access to a member without an actual instance of the the class. –  Colin D Aug 8 '12 at 20:36
@ColinD isn't abstract the keyword that you use when you intend other classes to inherit from the class? –  Sam I am Aug 8 '12 at 20:37
Yes, it can be used for that. You make the super class abstract so that it is impossible to create an instance of it, forcing the use of a subclass. –  Colin D Aug 8 '12 at 20:41
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer


