class design: When to use static methods?

Discussion in 'Java' started by Stefan Ram, Jul 2, 2013.

  1. Stefan Ram

    Stefan Ram Guest

    Instance methods! They make the difference between
    object-oriented programming in Java and procedural
    programming in Java (according to the common believes).
    And OOP is better than procedural programming, so we must
    use instance methods! (according to the common believes)

    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).

    What might have been the reason for which they have done this?
     
    Stefan Ram, Jul 2, 2013
    #1
    1. Advertising

  2. Stefan Ram

    Stefan Ram Guest

    -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.

    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.
     
    Stefan Ram, Jul 2, 2013
    #2
    1. Advertising

  3. Stefan Ram

    Arne Vajhøj Guest

    On 7/2/2013 11:04 AM, Stefan Ram wrote:
    > -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
     
    Arne Vajhøj, Jul 3, 2013
    #3
  4. Stefan Ram

    Arne Vajhøj Guest

    On 7/2/2013 10:47 AM, Stefan Ram wrote:
    > Instance methods! They make the difference between
    > object-oriented programming in Java and procedural
    > programming in Java (according to the common believes).
    > And OOP is better than procedural programming, so we must
    > use instance methods! (according to the common believes)


    That is an over simplification of OOP.

    GoF patterns are very OOP'ish and they use static methods
    for some things.

    > 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).
    >
    > What might have been the reason for which they have done this?


    Both my Java 7 and 8 docs has a Path interface and a class Files
    with a static exists method.

    So I am a bit confused by the above.

    All methods in Files are static utility methods that just
    use some other classes with the real functionality.

    Arne
     
    Arne Vajhøj, Jul 3, 2013
    #4
  5. Stefan Ram

    Eric Sosman Guest

    On 7/2/2013 10:47 AM, Stefan Ram wrote:
    > Instance methods! They make the difference between
    > object-oriented programming in Java and procedural
    > programming in Java (according to the common believes).
    > And OOP is better than procedural programming, so we must
    > use instance methods! (according to the common believes)


    Not so long ago in a thread about "The invokeLater loop,"
    you seemed quite pleased with a (perverted) use of the static
    method SwingUtilities.invokeLater(). Does this transgression
    against OOP principles bring a blush to your cheek?

    Or is the redness a result of blowing too much smoke?

    --
    Eric Sosman
    d
     
    Eric Sosman, Jul 3, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    1
    Views:
    549
    Scott Allen
    Jan 7, 2005
  2. SaravanaKumar
    Replies:
    6
    Views:
    9,518
    Tony Morris
    Oct 19, 2004
  3. Oliver Wong
    Replies:
    14
    Views:
    1,678
    Chris Uppal
    Jun 13, 2006
  4. Oltmans
    Replies:
    6
    Views:
    387
    Terry Reedy
    Mar 11, 2009
  5. Kenneth McDonald
    Replies:
    5
    Views:
    376
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page