Re: class design: When to use static methods?
On 7/2/2013 11:04 AM, Stefan Ram wrote:
ram@zedat.fu-berlin.de (Stefan Ram) writes:
Yet Sun or Oracle has moved the non-static exists() method
of java.nio.file.Path (of the early 1.7 versions of Java)
to the static exists() method of java.nio.file.Files (of the
current version of Java).
Another remarkable case is this:
Today, one needs to write:
java.nio.file.Files.readAttributes
( path, java.nio.file.attribute.BasicFileAttributes.class )
, that is, one has to use a kind of ?class token? (2nd
argument) to tell the static readAttributes method what to do.
This does not really use Java's type system and was also
changed since the prerelease version.
public <T> T methodname(Class<T> clz, SomeType arg)
is a very commonly used construct in Java.
And I don't see it as not using the type system.
A hypothetical version of this call that would use the
Java type system in a more natural way might be:
java.nio.file.attribute.BasicFileAttributes.of( path );
. It would not need a ?type token? to tell the method called
which kind of attributes to read, while it, too, still uses
a static method.
First BasicFileAttributes is an interface which makes it impossible
to do this in Java 1.7.
Second even it were a class then making a class its own factory
is rarely a good choice.
Arne