Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of protected modifier for static methods

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"?

like image 358
Andremoniy Avatar asked Apr 21 '15 12:04

Andremoniy


People also ask

What does the protected modifier do?

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package. The following table shows the access to members permitted by each modifier.

Can static methods be protected?

Actually there is nothing fundamentally wrong with protected static . If you really want a static variable or method that is visible for the package and all subclasses of the declaring class then go ahead and make it protected static .

What is the purpose of static methods?

A static method has two main purposes: For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.

Can we use protected with static in Java?

Scenario:two classes (super class and subclass) in different packages. Super class has two protected methods,static and nonstatic method. Subclass using reference on superclass can "see" protected static method but cannot see non static protected method...


1 Answers

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.

like image 115
dominicoder Avatar answered Oct 13 '22 00:10

dominicoder