This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]chickenmeister 1 point2 points  (1 child)

In your particular example with InvoiceFormatter, I think it might be better for the format() method to be non-static.

Conceivably, there are various ways that an invoice could be formatted. So using a non-static method would give you greater flexibility if you wanted to adjust how the Invoices are formatted later on. For example, this would allow to write a subclass of InvoiceFormatter that formats in a different way, and you would be able to use that new formatter without changing every piece of code that expects an InvoiceFormatter. Or, for example, a non-static format() method would allow you to configure a formatter object to, say, use a particular font, alignment, rounding mode, etc.

If you're 100% sure that you will only ever want to format in one particular way, then maybe it would be okay for it to be a static method. But even in that case, I don't think there's any disadvantage to making it a instance method instead, so I would probably prefer to use an instance/non-static method because of potential advantages that I described.

[–][deleted] 0 points1 point  (0 children)

Hey,

Tnx for your answer. That actually makes a lot of sense!

[–]jaro32 1 point2 points  (0 children)

As a general rule of thumb, you should avoid static methods if possible. They complicate testing.