[nycphp-talk] static methods in derived classes
Phillip Powell
phillip.powell at adnet-sys.com
Thu Sep 23 12:37:37 EDT 2004
I talked with a few Javaheads of the Northern Virginia Java Users Group
(NovaJUG) about this issue, and here is their reply regarding
declarations and static methods:
I think static methods are extremely useful, but they are widely
misunderstood. Many people access them via an instance. This is
misleading and can lead to surprising results. Intellij Idea suggests
that any such uses be changed to reference static methods by their
class name, and I think that's a good idea.
When a method does not rely on any instance context, it's a very good
idea to declare it static because:
1) it can (and should) be called without an instance, thereby saving
resources. (An exception to this is if it is called only within the
class itself, then it would be called within nonstatic methods of the
instance without a class name or a this. In this case, making it
static, even if private, does yield benefit #2).
2) more is communicated to the reader than if the method were
nonstatic; you are promising that nothing in this method relies on
instance context, and the compiler is enforcing your promise. This is
a cue to future readers that this method can easily be refactored,
perhaps moved to a utility class or even a different package.
The class below illustrates the problem. One would think that the
general rules of inheritance apply, and that the object's actual type
would determine which foo() is called. However, that is not the case;
it is the class of the declared reference that determines which method
is called.
Because the method is bound to the class itself, and not objects of
that class, it is wise to specify the class name (as in Meal::eat());
to do otherwise is to invite confusion.
public class StaticOverrideMethodTest {
static class Base {
static void foo() { System.out.println("I am a Base."); }
}
static class Derived extends Base {
static void foo() { System.out.println("I am a Derived."); }
}
public static void main(String [] args) {
Base b = new Derived();
b.foo();
Derived d = new Derived();
d.foo();
}
}
David Sklar wrote:
> csnyder wrote:
>
>> What Adam makes a lot of sense, and it implies that self:: is poorly
>> named.
>> self:: doesn't mean "self", it means "a static instance of this very
>> same class".
>>
>> I think you mean for a Meal/Lunch instance to chew() its own food,
>> which is whacky.
>
>
> Here's another bug report about the same issue. Since "self" is bound
> at compile time, it can't know about subclasses:
>
> http://bugs.php.net/bug.php?id=29647
>
> David
> _______________________________________________
> New York PHP Talk
> Supporting AMP Technology (Apache/MySQL/PHP)
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.newyorkphp.org
>
--
---------------------------------------------------------------------------------
Phil Powell
Multimedia Programmer
BPX Technologies, Inc.
#: (703) 709-7218 x107
Fax: (703) 709-7219
More information about the talk
mailing list