12

I found such an usage of protected modifier while searching for a solution for my other question: Ignoring case in strings with unitils ReflectionComparator

In the org.unitils.reflectionassert.ReflectionComparatorFactory class there is a method with the signature:

protected static List<Comparator> getComparatorChain(Set<ReflectionComparatorMode> modes)

But this is only a particular case.

After all we can always extend such any non-final class and "override" it's static protected method with the new public modifier. Say, we have a class A:

public class A {
    protected static void test() {
        // do some stuff
    }
}

and want to use it in another package:

public class UseA {
    private static class MyA extends A {
        public static void test() {
            A.test();
        }
    }

    void useA() {
        // A.test(); compile error, sure
        MyA.test();
    }
}

I concentrate my question on a general situation when some static method was declared as protected. I'm not asking about non-static fields or methods, because in some cases class can have a private constructor or a very complicated constructor with lots special params. But what is the purpose of such "hiding" static methods if entire class isn't final? Is such usage an OOP mistake or just a very weak "protection"?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • 2
    You can't override static methods. You can only re-define them in the sub class. – Chetan Kinger Apr 21 '15 at 13:02
  • 1
    @bot Sure, that is why I take this word "override" into quotes to underline my allegoric usage of this term – Andremoniy Apr 21 '15 at 13:03
  • 3
    http://stackoverflow.com/questions/24289070/why-we-should-not-use-protected-static-in-java – Murat Karagöz Apr 21 '15 at 13:03
  • @bot and in any case this your mention doesn't take off my question from order of day – Andremoniy Apr 21 '15 at 13:04
  • Non-static methods can be used the same way even if they're `protected`. – Bubletan Apr 21 '15 at 13:06
  • @Andremoniy It's quite possible that I am not able to grasp the question that you are asking. I want to understand what your question is? Are you asking what is the point of a protected static method in a class that is not final? Do you feel that any class that has a protected static method must be final? I don't see anything wrong with being able to access a protected method through inheritance (whether static or not) – Chetan Kinger Apr 21 '15 at 13:06
  • 3
    Is this really a duplicate? That other question refers to protected static variables, this one refers to protected static methods. – Necreaux Apr 21 '15 at 13:06
  • What is wrong in it. it is just like proxy. you cannot directly access that method so you are accessing it from an another class which has the access to it. – Rahul Thachilath Apr 21 '15 at 13:08
  • 1
    @bot I believe that any construction in programming language must have intelligible sence of usage. In case of `protected static` methods I can not catch such idea. – Andremoniy Apr 21 '15 at 13:14
  • @Necreaux and others, it is very nice that you have found my question as something new. I'll be glad if you check it to be reopened, if you think that it must be separate thread for discussin – Andremoniy Apr 21 '15 at 13:16
  • So you want to know whether `protected static` has any use case in the real world? – Chetan Kinger Apr 21 '15 at 13:16
  • @bot No, if you read my question carefully, you'll see my personal example or "real world" usage. I already don't know how explain you simplest essence of my question. It could be insuperable wall between us. – Andremoniy Apr 21 '15 at 13:18
  • Having `protected` methods in non-final class is a bit bad design, but it has use for final ones. – Bubletan Apr 21 '15 at 13:28

2 Answers2

3

But what is the purpose of such "hiding" static methods if entire class isn't final?

A protected static method would allow you to provide "utility" type functionality to derived classes, without exposing them in the public API where they might not make sense on their own.

I don't know the implementation of the getComparatorChain method you reference, but I imagine it's such a method. It would be marked static if it's not tied to any specific instance, and marked protected so as not to be a part of the public API, but also to allow derived classes to re-use the utility method in it's own implementation.

dominicoder
  • 9,338
  • 1
  • 26
  • 32
2

Overriding a static method reference means hiding it's implementation. See: Java Documentation's Overriding and Hiding Methods

Static Methods

If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.

The distinction between hiding a static method and overriding an instance method has important implications:

  • The version of the overridden instance method that gets invoked is the one in the subclass.
  • The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.
Patrick W
  • 1,485
  • 4
  • 19
  • 27