Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > class design: When to use static methods?

Reply
Thread Tools

class design: When to use static methods?

 
 
Stefan Ram
Guest
Posts: n/a
 
      07-02-2013
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?

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      07-02-2013
http://www.velocityreviews.com/forums/(E-Mail Removed)-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.

 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      07-03-2013
On 7/2/2013 11:04 AM, Stefan Ram wrote:
> (E-Mail Removed)-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

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      07-03-2013
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


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-03-2013
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"python.exe has stopped working" when os.execl() runs on Windows 7 cormogram@gmail.com Python 13 05-02-2013 12:50 PM
Warning in python file when i m using pychecker. Avnesh Shakya Python 2 04-26-2013 06:57 AM
when I read gzipped response from web-servers, GzipReader returnssometimes 'invalid compressed data -- crc error' henry.jykim@gmail.com Ruby 0 04-08-2013 03:51 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM



Advertisments