-2

As we all know the protected members there for inheritance (for future programmer's, not for the users of that class). On the other hand, static is definitely for users because we cannot override it, ...or something like that.

So why they decided to allow this over the old good compile-time error? :

public class MathUtils {

    protected static max(int a, int b) {
         return a > b ? a : b;
    }
}

Guys who downvote my question, I would appreciate if you can give me a good example from your real-wild practice, how did you used this approach to allow access to the static member in subclass of another package? Provide me with real example. I don't need much code, just a couple sentences of explanation. Thanks.

ieXcept
  • 1,088
  • 18
  • 37
  • 2
    Possible duplicate of [Difference among 'public', 'default', 'protected', and 'private'](http://stackoverflow.com/questions/215497/difference-among-public-default-protected-and-private) – WPrecht May 17 '16 at 18:25
  • Relevant http://stackoverflow.com/questions/24289070/why-we-should-not-use-protected-static-in-java – Tunaki May 18 '16 at 18:25

1 Answers1

6

On the other hand, static is definitely for users because we cannot override it

Overriding is not the only thing you can do to a method. Another important thing is having access to it.

When you declare a member protected static, it tells the compiler two things:

  • All classes deriving from your class will share this member, and
  • The only classes outside your package that would be granted access to this member are classes deriving from your class.

This is useful when you need to share logic or state among all derived classes, but protect the same logic from users of your class that do not extend it.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523