Why can't static methods be abstract in Java?

后端 未结 25 2278
不思量自难忘°
不思量自难忘° 2020-11-22 08:34

The question is in Java why can\'t I define an abstract static method? for example

abstract class foo {
    abstract void bar( ); // <-- this is ok
    ab         


        
相关标签:
25条回答
  • 2020-11-22 09:02

    An abstract class cannot have a static method because abstraction is done to achieve DYNAMIC BINDING while static methods are statically binded to their functionality.A static method means behavior not dependent on an instance variable, so no instance/object is required.Just the class.Static methods belongs to class and not object. They are stored in a memory area known as PERMGEN from where it is shared with every object. Methods in abstract class are dynamically binded to their functionality.

    0 讨论(0)
  • 2020-11-22 09:02

    First, a key point about abstract classes - An abstract class cannot be instantiated (see wiki). So, you can't create any instance of an abstract class.

    Now, the way java deals with static methods is by sharing the method with all the instances of that class.

    So, If you can't instantiate a class, that class can't have abstract static methods since an abstract method begs to be extended.

    Boom.

    0 讨论(0)
  • 2020-11-22 09:05

    Because "abstract" means: "Implements no functionality", and "static" means: "There is functionality even if you don't have an object instance". And that's a logical contradiction.

    0 讨论(0)
  • 2020-11-22 09:05

    The abstract annotation to a method indicates that the method MUST be overriden in a subclass.

    In Java, a static member (method or field) cannot be overridden by subclasses (this is not necessarily true in other object oriented languages, see SmallTalk.) A static member may be hidden, but that is fundamentally different than overridden.

    Since static members cannot be overriden in a subclass, the abstract annotation cannot be applied to them.

    As an aside - other languages do support static inheritance, just like instance inheritance. From a syntax perspective, those languages usually require the class name to be included in the statement. For example, in Java, assuming you are writing code in ClassA, these are equivalent statements (if methodA() is a static method, and there is no instance method with the same signature):

    ClassA.methodA();
    

    and

    methodA();
    

    In SmallTalk, the class name is not optional, so the syntax is (note that SmallTalk does not use the . to separate the "subject" and the "verb", but instead uses it as the statemend terminator):

    ClassA methodA.
    

    Because the class name is always required, the correct "version" of the method can always be determined by traversing the class hierarchy. For what it's worth, I do occasionally miss static inheritance, and was bitten by the lack of static inheritance in Java when I first started with it. Additionally, SmallTalk is duck-typed (and thus doesn't support program-by-contract.) Thus, it has no abstract modifier for class members.

    0 讨论(0)
  • 2020-11-22 09:08

    because if you are using any static member or static variable in class it will load at class loading time.

    0 讨论(0)
  • 2020-11-22 09:09

    A static method, by definition, doesn't need to know this. Thus, it cannot be a virtual method (that is overloaded according to dynamic subclass information available through this); instead, a static method overload is solely based on info available at compile time (this means: once you refer a static method of superclass, you call namely the superclass method, but never a subclass method).

    According to this, abstract static methods would be quite useless because you will never have its reference substituted by some defined body.

    0 讨论(0)
提交回复
热议问题