3

From what I understand, usually the static method should be called using class's reference or it can be called directly without reference if its in a static method or static block.

But does this apply when static method is called from child class static blocks?

Why it allows such thing, as static methods are not inherited, it should only be allowed using parent class name right?

public abstract class abs {

    /**
     * @param args
     */
    abstract void m();
    static void n(){
        System.out.println("satic method");
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}
class myclass extends abs{

    @Override
    void m() {
        // TODO Auto-generated method stub

    }
    static{
        n();
    }
}

Why my child class static block can call parent class static method without reference or classname?

xingbin
  • 27,410
  • 9
  • 53
  • 103
amarnath harish
  • 945
  • 7
  • 24

5 Answers5

4

Static method n() is inherited by subclass myclass, so you can call it directly in the static block of myclass.

xingbin
  • 27,410
  • 9
  • 53
  • 103
  • doesnt it violate the whole concept of "static belongs to class hence cannot be inherited" ? – amarnath harish Mar 25 '18 at 14:47
  • 1
    @amarnathharish [Are static methods inherited in Java?](https://stackoverflow.com/q/10291949), especially https://stackoverflow.com/a/29725529/1393766 – Pshemo Mar 25 '18 at 14:50
  • 2
    "The only difference with inherited static (class) methods and inherited non-static (instance) methods is that when you write a new static method with the same signature, the old static method is just hidden, not overridden." this makes sense – amarnath harish Mar 25 '18 at 14:56
  • @amarnathharish There is no such concept. You are mistaken. – user207421 Mar 29 '18 at 01:13
3

Usually the static method should be called using class's reference or it can be called directly without reference if its in a static method or static block.

Not really. For example an instance method can invoke a static method without prefixing the class.

More generally, static members (fields as methods) have to be invoked by prefixing their class only as the compiler cannot infer the class where they belong to.
As you invoke a static method defined in the parent class from a subclass (and static methods are inherited in the subclasses), you don't need to prefix the class of the method invocation as the compiler infer that.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 1
    okay i have one question, if static methods are inherited what if i also have another static method in the childclass with same method signatures ? how will the compiler resolve that? – amarnath harish Mar 25 '18 at 14:49
  • 1
    In this case, the method defined in the subclass shadows (and not overrides) which one defined in the parent. Note that it is generally a bad practice. Without any prefix, in the class context the compiler will choose the method of the current class. – davidxxx Mar 25 '18 at 14:55
0

Because you inherited the parent class, you have access to all non private members of that class directly as if it belonged to the child class.

Rob
  • 2,618
  • 2
  • 22
  • 29
0

Why it allows such thing

By inheritance.

as static methods are not inherited

You keep saying that. You're mistaken. From JLS #8.4.8:

A class C inherits from its direct superclass all concrete methods m (both static and instance) of the superclass for which all of the following are true: ...

For continuation see here.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

All the members of superclass are inherited by subclass, which includes static methods too.

    class SuperClassA {

    static void superclassmethod() {
        System.out.println("superclassmethod in Superclass ");
    }
}

public class SubClassA extends SuperClassA {
    static {
        superclassmethod();
    }

    public static void main(String[] args) {

    }
}

But when a static method of superclass is override it hides the superclass static method not overrides it.

Nisrin Dhoondia
  • 145
  • 1
  • 10
  • There is no such thing as overriding of static methods. – user207421 Mar 29 '18 at 10:02
  • @ EJP What I meant was if one writes in subclass a static method with the same name as that of the static method existing in superclass, then in this case the subclass method is not overriding the superclass static method but it hides the superclass static method. And also the runtime Polymorphism does not get applied when it is static method. – Nisrin Dhoondia Mar 29 '18 at 12:15