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 ?

user1203861
  • 1,177
  • 3
  • 15
  • 19
  • 1
    What do you mean by "static or dynamic"? A `class` can be declared with the `static` modifier iff [it's an static nested class](http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html). Please show code and attempt to explain what your question is again. – obataku Aug 08 '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 08 '12 at 20:21
  • http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods – Colin D Aug 08 '12 at 20:26

4 Answers4

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

Community
  • 1
  • 1
Colin D
  • 5,641
  • 1
  • 23
  • 35
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.

Community
  • 1
  • 1
  • `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 08 '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 says Reinstate Monica Aug 08 '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 08 '12 at 20:41
0

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

Edmon
  • 4,752
  • 4
  • 32
  • 42
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.

Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86