-1

If static members are not inherited in Java then why access modifiers (private, protected…) are applicable to them?

Perhaps, I’m not 100% clear on the term “inherited”: when they say “statics not inherited” do they mean “not visible” or “can not be redefined/reimplemented” or both?

Would it be correct to say that access modifiers imply only visibility when speaking about static class members, and visibility as well as reimplementation possibility when speaking about regular class members?

Can you please provide examples when making static, say, private makes sense?

Thanks

PS: Now I think that the statement "Static variables in Java are not inherited, they exist only in the class which declares them" spread here and there and around on this forum (for instance: What are the rules dictating the inheritance of static variables in Java?) IS NOT correct as

The Java Language Specification #8.4.8 states:

8.4.8 Inheritance, Overriding, and Hiding

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:

m is a member of the direct superclass of C. m is public, protected, or declared with package access in the same package as C. No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of m.

Community
  • 1
  • 1
  • 2
    `private` simply means that it cannot be accessed from outside the class. That applies just as much to static members as to instance members. – khelwood May 01 '17 at 22:13

3 Answers3

2

If static members are not inherited in Java then why access modifiers (private, protected…) are applicable to them?

Who said that static members cannot be inhertied, refer following JLS (§Example 8.2-2). example

In Java being "static" simply means that there is no object required to access this field/method.

"private" is just an access modifier which means that following field/method cannot be accessed outside of its, and can only accessed by this class itself or any inner class.

Only thing with "static" methods of a superclass inherited in subclass can be read understood from below excerpt from JLS:

If a class C declares or inherits a static method m, then m is said to hide any method m', where the signature of m is a subsignature (§8.4.2) of the signature of m', in the superclasses and superinterfaces of C that would otherwise be accessible to code in C.

So, basically you cannot override a static method but you can certainly inherit a static method, if it is not hidden by subclass.

Read following JLS section (§8.4.8.2. Hiding (by Class Methods)) about hiding of class methods.

Also, you can read Java's inheritance tutorial from here and below is a screen shot of summary from same page

enter image description here

hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
  • ... AND HIDING MEANS, I believe, that if static method is redefined in subclass then polymorphism does not work - which version is called is defined by the type of the reference variable and not by the actual object type – Andrey M. Stepanov May 01 '17 at 22:57
  • @AndreyM.Stepanov You got it. – hagrawal7777 May 02 '17 at 12:15
  • Well, `static` methods declared in an `interface` are not inherited, but that’s the only exception and it’s a new rule as `static` methods in `interface`s are possible since Java 8. – Holger May 09 '17 at 15:43
0

Member visibility doesn't just apply to child classes. From Oracle, they apply, in reference to a static member, to the rest of the package, or the rest of the "world".

If you create a private static member, you're stating that this is a member exclusive to that specific class. You then work your way up the visibility chain:

  • private is least visible,
  • <no modifier> (or package-protected) is second-least visible,
  • protected is second-most visible, and
  • public is most visible.

I've not seen anyone really use a protected static member, even though it's valid Java. Convention dictates that this should be a visible constant (in that case it's likely public), or a less-than visible constant for test exposure (which would justify package-protected visibility.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • A `protected static` member can be useful, e.g., for a nested class used by subclasses for some implementation task. – Lew Bloch May 01 '17 at 23:06
0

A static member in a class simply means that it will be shared across all the objects of the class. A new variable won't be instantiated for every instance of the class.

Access modifiers control the visibility of variables in Java. The meaning of the access modifiers is the same for static as well as non-static members. It has nothing to do with static members only.

With respect to your question, the use of access modifiers in static variables means the following:

private static count: The static member count is only visible to the class defining it. However, if you inherit the class defining this static variable, your inherited class cannot directly access it. count can be accessed only if super class has a getter defined for it, or if the access modifier is changed to public or protected.

qrius
  • 621
  • 2
  • 9
  • 22
  • As it turns out a static member class is not only shared by all instances of the class but can also be accessed by any child class ... if access level does not restrict the visibility – Andrey M. Stepanov May 01 '17 at 22:25
  • Yes. Check this out: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – qrius May 01 '17 at 22:27
  • "Static fields are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory." – qrius May 01 '17 at 22:29
  • "As it turns out", a static member type obeys the same accessibility and inheritance rules as other static members. So `private` members are not inherited, package-private only by subclasses in the same package, and the other levels are inherited. – Lew Bloch May 01 '17 at 23:05