2

The following code demonstrates that the subclass named SubClass has a direct access to a final static synchronized method named staticMethod. There is no need to associate it with its class name.

package synchronizedpkg;

class SuperClass
{
    public final static synchronized void staticMethod()
    {
        System.out.println("Method called.");
    }
}

final class SubClass extends SuperClass
{
    public void woof()
    {
        staticMethod();
    }
}

final public class Main
{
    public static void main(String[] args)
    {
        new SubClass().woof();
    }
}

This is somewhat confusing in terms of inheritance because a final method can not be inherited and consequently should not directly be accessed by it's subclasses. How does a final static method as shown above have a direct access from its child class?

user
  • 5,335
  • 7
  • 47
  • 63
Lion
  • 18,729
  • 22
  • 80
  • 110
  • Remove both the final and the synchronized modifiers. They are not related to this issue, and the bottom paragraph of the post still applies. I am surprised there are no warnings? ;-) In any case, I'd argue "a poor language design decision", compare with `myThread.sleep(42)` <-- what *really happens*? I know Jon Skeet has a few answers dealing with this... –  Nov 16 '11 at 20:12
  • As far as "when" to use `final static`, see [Behaviour of final static method](http://stackoverflow.com/questions/1743715/behaviour-of-final-static-method) –  Nov 16 '11 at 20:18
  • 3
    "a final method can not be inherited," is wrong. Final methods are inherited as long as they are not static. Fact is that they can't be overriden. – Bhesh Gurung Nov 16 '11 at 20:22

5 Answers5

2

The static method is inherited just like instance methods. From section 8.4.8 of the JLS:

A class C inherits from its direct superclass and direct superinterfaces all non-private methods (whether abstract or not) of the superclass and superinterfaces that are public, protected or declared with default access in the same package as C and are neither overridden (§8.4.8.1) nor hidden (§8.4.8.2) by a declaration in the class.

That doesn't say anything about only inheriting instance methods.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Your understanding is wrong:

This is somewhat confusing in terms of inheritance because a final method can not be inherited and consequently should not directly be accessed by it's subclasses. How does a final static method as shown above have a direct access from its child class?

A final method cannot be overridden, or in the case of a static method hidden, it is still inherited. If you want to prevent the subclass from seeing it, then you must make it private.

So, it is visible, hence you can access it. And it is a member, hence you don't need to qualify it.

sceaj
  • 1,573
  • 3
  • 12
  • 24
  • I guess the static method could also be default access to hide it from subclasses in other packages. – sceaj Nov 16 '11 at 20:29
1

Static means which is same for all objects. So, it doesn't really matter if you call it with object reference or class reference because it is same for every object.

Example: Suppose you have teacher who teaches to all the students in all grades. So, it doesnt really matter if 1st grade student calls her 1st grade teacher and 2nd grade student calls her 2nd grade teacher or somebody else calls her school teacher. They are all refering to same teacher.

Students can refer to her by name and every other student can know that he is refering to her. But people from outside school, if they have to refer to her they have to say the school name and teachers name.

That's how it works even in java. Java is probably the only language which relates to real world scenarios. Hope this helps..... :)

user2626445
  • 1,031
  • 12
  • 27
1

From Understanding Instance and Class Members:

"The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in

ClassName.methodName(args)

Note: You can also refer to static methods with an object reference like

instanceName.methodName(args)

but this is discouraged because it does not make it clear that they are class methods."

Hence, you can call a static method from an instance that inherits it OR from its class name. This means that for all intents and purposes, you can imagine the above method as not being static, and you can inherit it normally.

EDIT: As to the final modifier, this just means that the method cannot be overridden in a subclass. Far as I can see, you aren't doing that, so it does not affect the outcome. You can read about this here.

SuperTron
  • 4,203
  • 6
  • 35
  • 62
  • Yes, +1: but how does (or does not) `final` work with `static` methods? –  Nov 16 '11 at 20:20
  • A final method means that it cannot be overridden. Thats all that it does. So you have a function now that can't be overridden in subclasses and can be called via its class name. – SuperTron Nov 16 '11 at 20:21
  • static methods cannot be overridden, but they can be hidden in subclasses. See my answer. – sceaj Nov 16 '11 at 20:35
0

static means class level, it has nothing to do with the objects of the class, you don't need a n object to access static methods

daydreamer
  • 87,243
  • 191
  • 450
  • 722