When would you declare a method as static?

  • Follow


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I read on various websites that a method is generally declared as
static if it `achieves some computations.' Well, this is fairly vague,
and I would have the tendency to declare a method as static if it
produces the same result for every instance of the class (that is,
that it produces some variable that might be declared as static).

Any help would be greatly appreciated.
- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

A thief thinks everyone steals.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk2PqtsACgkQM0LLzLt8MhxE5ACgl28sQyPVuM4qg/k9rUBZq9Uj
64QAn1d8TydKlG8Q5qy0RmmMUvW+7wBk
=aTO8
-----END PGP SIGNATURE-----
0
Reply Luca.Merciadri (232) 3/27/2011 9:23:39 PM

On Sun, 27 Mar 2011 23:23:39 +0200, Merciadri Luca
<Luca.Merciadri@student.ulg.ac.be> wrote, quoted or indirectly quoted
someone who said :

>I read on various websites that a method is generally declared as
>static if it `achieves some computations.

If you use the IntelliJ Idea IDE, it will suggest some methods be
converted to static.  It decides simply on the basis of a lack of
references to instance methods or fields.

Static methods invoke faster.  Instance methods may be overridden by a
subclass.

Static methods are methods on the whole class, the all objects of that
class, or about no one object in particular.  Instance objects are
about one particular instance of the class.

see http://mindprod.com/jgloss/static.html
http://mindprod.com/jgloss/instance.html
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
There are only two industries that refer to their customers as "users".
~ Edward Tufte

0
Reply Roedy 3/28/2011 12:39:38 AM


On 03/27/2011 05:23 PM, Merciadri Luca wrote:
> I read on various websites that a method is generally declared as
> static if it `achieves some computations.' Well, this is fairly vague,

Those are bad sources.

All methods "achieve some computations".

> and I would have the tendency to declare a method as static if it
> produces the same result for every instance of the class (that is,
> that it produces some variable that might be declared as static).
>
> Any help would be greatly appreciated.

A static member is one that belongs to the entire class.  An instance member 
is one that belongs to an instance.  A method is a member.  A static method 
belongs to the entire class.  It cannot directly refer to instance members 
because it is not tied to an instance.

  package example;
  public class Example
  {
    public static void useful( Example ex )
    {
      foo.run(); // ILLEGAL!  tried to use an instance member
      instant(); // ILLEGAL!  tried to use an instance member

      ex.foo.run(); // LEGAL! accessed through an instance
      ex.instant(); // LEGAL! accessed through an instance
    }

    /* p-p */ final Foo foo = new Foo(); // assume a run method

    public void instant()
    {
    }
  }

The "produces the same result" rule of thumb will not work.  A static method 
can produce all kinds of results.  The rule of thumb is whether it needs state 
specific to an instance.  If not it can be static.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
0
Reply Lew 3/28/2011 2:14:30 AM

Merciadri Luca wrote:

> Hi,
> 
> I read on various websites that a method is generally declared as
> static if it `achieves some computations.' Well, this is fairly vague,
> and I would have the tendency to declare a method as static if it
> produces the same result for every instance of the class (that is,
> that it produces some variable that might be declared as static).
> 

I think a good example is Java's Math class (java.lang.Math). All of
its methods are static, but let's just consider sqrt().

If you want to take the square root of 5.0:

double answer = Math.sqrt(5.0);

In this case, you are saying "go to the Math class and execute the sqrt
method on some double value I give it," you don't have to have any
object instantiated -- no objects are involved here.

If sqrt() were not static, it would need an object instance to operate
on, so it would probably look something like this:

double answer = new Double(5.0).sqrt();

Notice how, in this case, you would have to take the double literal
5.0, make a Double object out of it, and then apply the sqrt() method
to that object instance. Also, sqrt() would have to be a member method
of the Double class.

There is a parallel here with a Java program's main method, which is
always declared static. This is because your main method does not
(cannot!) operate on an object. If it did, you would have to
instantiate an object of the class containing your main method -- let's
say it's "Program" -- to get access to a main method to run...but to
instantiate a program object in the first place, you'd have to have
some entry point for your code to start at (i.e. main), but your main
needs an instance of Program... etc., etc., ad nauseum.

0
Reply Alex 3/28/2011 5:26:22 AM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks all.

- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

Absence sharpens love, presence strengthens it.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk2QSO0ACgkQM0LLzLt8Mhz1pgCdEyyAuD4S87UBrjbXxUKJFf1b
B7sAnRNWYC4AD9rs3BFBdNVpn6oPqyxz
=cDza
-----END PGP SIGNATURE-----
0
Reply Merciadri 3/28/2011 8:38:05 AM

"Alex Mentis" <foo@invalid.invalid> writes:
>double answer = Math.sqrt(5.0);
>In this case, you are saying "go to the Math class and execute the sqrt
>method on some double value I give it," you don't have to have any
>object instantiated -- no objects are involved here.

  In a generalized sense of the word �object�, a temporary
  object is created. In computer science, it is called
  �activation record�; the method is �incarnated�.
  (There is memory allocated for the parameters and local
  variables of this method instance.)

  A static method is a special case of a generalized �class�,
  doubling as its own constructor and with only one method.

>new Double(5.0).sqrt();

  Yes, something like this happens, �new Double(5.0)� makes
  the creation of the activation record explicit.

>always declared static. This is because your main method does not
>(cannot!) operate on an object. If it did, you would have to
>instantiate an object of the class containing your main method -- let's
>say it's "Program" -- to get access to a main method to run...but to
>instantiate a program object in the first place, you'd have to have
>some entry point for your code to start at (i.e. main), but your main
>needs an instance of Program... etc., etc., ad nauseum.

  Well, IIRC, applets can be started without a static main
  method. The same approach could have been chosen for console
  programs as well.

0
Reply ram 3/28/2011 12:30:21 PM

Stefan Ram wrote:
> Alex Mentis writes:
>> double answer = Math.sqrt(5.0);
>> In this case, you are saying "go to the Math class and execute the sqrt
>> method on some double value I give it," you don't have to have any
>> object instantiated -- no objects are involved here.

>    In a generalized sense of the word »object«, a temporary
>    object is created. In computer science, it is called
>    »activation record«; the method is »incarnated«.
>    (There is memory allocated for the parameters and local
>    variables of this method instance.)
>
>    A static method is a special case of a generalized »class«,
>    doubling as its own constructor and with only one method.

Actually, doesn't a static method inhere to the class object itself, rather 
than create an /ad hoc/ instance of something?

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
0
Reply Lew 3/28/2011 1:09:38 PM

Stefan Ram wrote:

> "Alex Mentis" <foo@invalid.invalid> writes:
> > double answer = Math.sqrt(5.0);
> > In this case, ... you don't have to have
> > any object instantiated -- no objects are involved here.
> 
>   In a generalized sense of the word �object�, a temporary
>   object is created. In computer science, it is called
>   �activation record�; the method is �incarnated�.
>   (There is memory allocated for the parameters and local
>   variables of this method instance.)

I was not using "object" in as general a sense as you appear to be. I
was referring to objects as they are typcially thought of in
object-oriented programming. When a C function is called, an activation
record is pushed onto the stack, but nobody would correctly call C a
language that provides "objects" in the object-oriented sense.

> > new Double(5.0).sqrt();
> 
>   Yes, something like this happens, �new Double(5.0)� makes
>   the creation of the activation record explicit.

I was not saying that anything like the above happens "behind the
scenes." Allocating new memory for a Double object is not the same as
pushing an activation record on the stack. I was simply illustrating
the syntax that would be required, were java.lang.Math.sqrt() not
static.

>   Well, IIRC, applets can be started without a static main
>   method. The same approach could have been chosen for console
>   programs as well.

IIUC, applets don't have main methods, they have a start() method. It
is not static, but then again, applets need to be run in a browser or
AppletViewer - they can't run as standalone applications. I suppose the
browser or AppletViewer instantiates an object of the applet's class,
thus allowing its start() method to be called as a member method of the
instantiated applet object.

0
Reply Alex 3/28/2011 5:26:45 PM

7 Replies
278 Views

(page loaded in 0.077 seconds)

4/27/2013 12:22:50 AM


Reply: