John Tsioumpris wrote:Currently - mainly because my programming background is VBA - i have used the static way of an auxiliary method so that code is used throughout the application ...but i am not sure if this is the best practice.
On the whole I'd say probably not, but without a specific example it's difficult to say for certain.
Java is an
Object-Oriented language, so it generally works best when you have
objects (ie, instances) cooperating with each other. However, there are cases (for example, the
java.lang.Math,
java.util.Arrays and
java.util.Collections classes in the Java SE), that are essentially sets of
static utility methods, and it's a perfectly reasonable alternative in
some cases.
It should, however, be added that
Math.random() (a "catch-all" random number generator method) has since been superceded by the far superior
Random class which allows you to generate
RNG instances for your own use.
So, in answer to your question - and without more detail - my answer would be:
Prefer instance methods to
static ones, and utility
objects to static utility classes.
About the only exception I can think of is if you have a method that is
wholly self-contained, and my
test would go something like this:
If I can write a
static method with an
identical signature to an instance one, and it works just as well, then I
might consider making it
static; but not otherwise.
However, there are exceptions to
that general rule too (there always are in programming
) -
Arrays being a good case in point.
HIH
Winston
PS: Welcome to the Ranch, John; and I hope you had a good Christmas too.