127

I was going through this question Is there a way to override class variables in Java? The first comment with 36 upvotes was:

If you ever see a protected static, run.

Can anyone explain why is a protected static frowned upon?

Community
  • 1
  • 1
Zeeshan
  • 9,465
  • 14
  • 65
  • 93
  • 6
    There's nothing wrong with a protected static field, as long as it's `final`. A mutable static field shared across classes is definitely cause for worry. Multiple classes updating a static field is not likely to be reliable or easy to follow, especially since the presence of any protected field or method implies that the class is meant to be extended by classes in other packages, possibly classes not under the control of the author of the class containing the protected field. – VGR Jun 18 '14 at 15:25
  • 6
    @VGR, `final` doesn't means the field is immutable. You can always modify the `object` referenced by a `final` reference variable. – Zeeshan Jun 18 '14 at 16:49
  • @VGR I disagree. The ONLY reason you would make a static variable is to have access to it from within another package only by inherience, and access to a single field should NOT be the reason for inheritence. It's a flawed design, IMO, and if you resort to that, you should probably rethink your application's structure. That's just my opinion though. – Dioxin Jun 18 '14 at 17:01
  • @LoneRider You're right. I was thinking immutable, and final certainly doesn't guarantee that. – VGR Jun 18 '14 at 17:11
  • Even I came here from the same question. – Raj Rajeshwar Singh Rathore May 05 '19 at 02:47

8 Answers8

89

It's more a stylistic thing than a direct problem. It suggests that you haven't properly thought through what is going on with the class.

Think about what static means:

This variable exists at class level, it does not exist separately for each instance and it does not have an independent existence in classes which extend me.

Think about what protected means:

This variable can be seen by this class, classes in the same package and classes which extend me.

The two meanings are not exactly mutually exclusive but it is pretty close.

The only case I can see where you might use the two together is if you had an abstract class that was designed to be extended and the extending class could then modify the behavior using constants defined in the original. That sort of arrangement would most likely end up very messy though and indicates weakness in the design of the classes.

In most cases it would be better to have the constants as public since that just makes everything cleaner and allows the people sub-classing more flexibility. Quite apart from anything else in many cases composition is preferable to inheritance, while abstract classes force inheritance.

To see one example of how this could break things and to illustrate what I mean by the variable not having an independent existence try this example code:

public class Program {
    public static void main (String[] args) throws java.lang.Exception {
        System.out.println(new Test2().getTest());
        Test.test = "changed";
        System.out.println(new Test2().getTest());
    }
}

abstract class Test {
    protected static String test = "test";
}

class Test2 extends Test {
    public String getTest() {
        return test;
    }
}

You will see the results:

test
changed

Try it yourself at: https://ideone.com/KM8u8O

The class Test2 is able to access the static member test from Test without needing to qualify the name - but it does not inherit or get its own copy. It is looking at the exact same object in memory.

Tim B
  • 38,707
  • 15
  • 73
  • 123
  • 4
    You guys are hung up on inheritance. The typical case where this is seen is someone wanting package access without making it public (e.g. a unit test). – spudone Jun 18 '14 at 17:08
  • 3
    @spudone but unit tests are generally placed in the same package. To give them access you just use the default (package) access level. Protected gives access to subclasses too which is not required or relevant to unit test. – Tim B Jun 25 '14 at 08:27
  • While it answer correctly the question, I don't personally get this line of code: `Test.test = "changed";`. How can the `class Program` be able to access the *protected* attribute `test`? Shouldn't *protected* attributes be accessible just from children of the *Test* class? Or it is because the *protected* can be seen from *"classes in the same package"*, as quoted above? (sorry if the question is stupid; I almost don't know Java) – Kamafeather Oct 08 '14 at 13:35
  • 1
    @Kamafeather Protected means that children can see you from different classes and any classes in the same package can see you. So yes Program is in the same package and hence can see the protected member. – Tim B Oct 08 '14 at 13:38
  • Thank you for confirming, and excuse me if I came out with this noob question; I usually work in PHP and accessing *protected* like that would not have sense there. – Kamafeather Oct 08 '14 at 15:25
  • 2
    "*the extending class could then modify the behavior*" This would violate the Liskov Subsitution principle. A subclass should not modify the behavior of it's supertype. This falls under the whole "a square is not a rectangle" argument: adjusting the superclass (`Rectangle`) to adjust width/height to ensure equality will (for `Square`) will produce undesired results if you were to replace every supertype with an instance of it's subtype. – Dioxin Mar 03 '16 at 20:54
  • @VinceEmigh I'm not sure what you are talking about? *"the extending class could then modify the behavior"* doesn't appear in my answer... – Tim B Mar 04 '16 at 16:52
  • 1
    "*The only case I can see where you might use the two together is if you had an abstract class that was designed to be extended and **the extending class could then modify the behavior using constants defined in the original***" A bit hidden, but it's in there. The code example also expresses the statement – Dioxin Mar 04 '16 at 16:58
  • @VinceEmigh Oh I see what you mean. Yes it's a bad idea. I was trying to think of any possible case where you might use the two modifiers together and have legitimate (if badly designed) code and that was all I could come up with. – Tim B Mar 04 '16 at 17:26
  • @TimB That's sounds like encouraging sloppy code, which I doubt is your intention. It would be nice if you updated your answer, as I've had a few students link me to your post, just for me to link them to your comment. Could keep the upvotes while encouraging sturdier/clearer code. – Dioxin Jan 10 '20 at 04:42
  • @VinceEmigh Better? – Tim B Jan 10 '20 at 11:58
33

It's frowned upon because it's contradictive.

Making a variable protected implies it will be used within the package or it will be inherited within a subclass.

Making the variable static makes it a member of the class, eliminating the intentions of inheriting it. This leaves only the intention of being used within a package, and we have package-private for that (no modifier).

The only situation I could find this useful for is if you were declaring a class that should be used to launch the application (like JavaFX's Application#launch, and only wanted to be able to launch from a subclass. If doing so, ensure the method is also final to disallow hiding. But this is not "the norm", and was probably implemented to prevent adding more complexity by adding a new way to launch applications.

To see the access levels of each modifier, see this: The Java Tutorials - Controlling Access to Members of a Class

Dioxin
  • 13,042
  • 5
  • 35
  • 70
  • 4
    I don't understand how would `static` eliminate the intentions of inheriting it. Because my subclass in another package still require the field of super to be `protected` to access, even though it's `static`. `package-private` cannot help – Aobo Yang Apr 02 '15 at 07:47
  • @AoboYang You're right, which is why some people use `protected static`. But it's a code smell, hence the "*run*" part. Access modifiers and inheritance are two different subjects. Yes, you won't be able to access a static member from a super class if it was `package-private`. But you shouldn't rely on inheritance to reference `static` fields in the first place; it's a sign of poor design. You'll notice attempts at overriding `static` methods give no results, which is a clear sign that inheritance is not class based. If you need access outside of the class or package, it should be `public` – Dioxin Apr 02 '15 at 08:07
  • 3
    I have a class with some `private static` util functions at first, but I think someone may want to improve or customize from my class and these util functions may also provide convenience to them. `public` may not be appropriate since util methods are not for the user of the instances of my class. Could you help me figure out a good design instead of `protected`? Thanks – Aobo Yang Apr 02 '15 at 09:09
  • In that link, it says 'Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.' ,which is contradict from this question, any thoughts? – Cecilia Nov 08 '19 at 13:43
14

I don't see a particular reason why this should be frowned upon. There may always be alternatives to achieve the same behavior, and it will depend on the actual achitecture whether these alternatives are "better" than a protected static method or not. But one example where a protected static method would be reasonable, at least, could be the following:

(Edited to split into separate packages, to make the use of protected clearer)

package a;
import java.util.List;

public abstract class BaseClass
{
    public Integer compute(List<Integer> list)
    {
        return computeDefaultA(list)+computeDefaultB(list);
    }

    protected static Integer computeDefaultA(List<Integer> list)
    {
        return 12;
    }
    protected static Integer computeDefaultB(List<Integer> list)
    {
        return 34;
    }
}

Derived from that:

package a.b;

import java.util.List;

import a.BaseClass;

abstract class ExtendingClassA extends BaseClass
{
    @Override
    public Integer compute(List<Integer> list)
    {
        return computeDefaultA(list)+computeOwnB(list);
    }

    private static Integer computeOwnB(List<Integer> list)
    {
        return 56;
    }
}

Another derived class:

package a.b;

import java.util.List;

import a.BaseClass;

abstract class ExtendingClassB extends BaseClass
{
    @Override
    public Integer compute(List<Integer> list)
    {
        return computeOwnA(list)+computeDefaultB(list);
    }

    private static Integer computeOwnA(List<Integer> list)
    {
        return 78;
    }
}

The protected static modifier can certainly be justified here:

  • The methods can be static, because they do not depend on instance variables. They are not intended to be used directly as a polymorphic method, but rather are "utility" methods that offer default implementations that are part of a more complex computation, and serve as "building blocks" of the actual implementation.
  • The methods should not be public, because they are an implementation detail. And they can't be private because they should be called by the extending classes. They also can't have "default" visibility, because then they will not be accessible for the extending classes in other packages.

(EDIT: One could assume that the original comment only referred to fields, and not to methods - then, however, it was too general)

Marco13
  • 50,927
  • 9
  • 71
  • 148
  • In this case, you should define the default implementations as `protected final` (since you don't want them overridden), not `static`. The fact that a method doesn't use instance variables does not mean it should be `static` (although it *can*). – barfuin Jun 18 '14 at 16:11
  • 2
    @Thomas Sure, in this case, this would also be possible. In general: This certainly is partially subjective, but my rule of thumb is: When a method is not intended to be used polymorphically, and it can be static, then I make it static. In contrast to making it `final`, it does not only make clear that the method is not intended to be overridden, it *additionally* makes clear to the reader that the method does not use instance variables. So succinctly: There simply is no reason to *not* make it static. – Marco13 Jun 18 '14 at 16:16
  • 1
    *When a method is not intended to be used polymorphically, and it can be static, then I make it static.* - This will get you in trouble when you start using mock frameworks for unit testing. But this leads us to a different topic ... – barfuin Jun 18 '14 at 16:20
  • @Marco13 there's also the case when you make something `protected`, not to show inheritance, but to keep it under a single package - not exposed. I guess sealed interfaces would become helpful in this manner when they will in java – Eugene Feb 04 '18 at 19:49
  • @Eugene That comment irritates me a bit. Package-visibility can be achieved without any modifier, whereas `protected` means "Inheriting classes (even in different packages)"... – Marco13 Feb 04 '18 at 21:18
  • @Marco13 you're right, should have made this a better one. I was a referring to something along the lines of `@VisibleForTesting`... – Eugene Feb 04 '18 at 21:20
7

Static members are not inherited, and protected members are only visible to subclasses (and of course the containing class), so a protected static has the same visibility as static, suggesting a misunderstanding by the coder.

Bohemian
  • 365,064
  • 84
  • 522
  • 658
  • 1
    Could you please provide a source for your first statement? In a quick test, a protected static int was inherited to and reused in a subclass without a problem. – hiergiltdiestfu Jun 18 '14 at 15:23
  • Protected static has the same visibility as package-private static, not private static. To add onto this, if you're using protected and static, its best to just remove the access modifier to make it package-private (if your intentions were to make it accessable within the package) – Dioxin Jun 18 '14 at 15:26
  • 2
    Uhm ... no. package private and `protected` are not the same. If you just say `static`, the field is only visible to subclasses in the same package. – Aaron Digulla Jun 18 '14 at 15:27
  • 1
    @AaronDigulla `protected static` allows access *within the package* or *from a subclass*. Making a variable static removes the intentions of subclassing, leaving the only intention being access from within the package. – Dioxin Jun 18 '14 at 15:30
  • @VinceEmigh: Sorry, I was talking to Bohemian. Should have made this more clear. – Aaron Digulla Jun 18 '14 at 15:44
  • @VinceEmigh: I would think that making it static would imply that code outside the package would have to include its own derived class in order to access the variable. From a security perspective, if a class allows outside derivation, `protected static` would be equivalent to `public static`. On the other hand, `protected static` would convey something of an implication that outside code unaffiliated with the class *shouldn't* access the variable, even if it wouldn't be completely prevented from doing so. – supercat Jun 18 '14 at 20:02
5

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.

Some people generally avoid to use protected for various reasons and some people think non-final static variables should be avoided by all means (I personally sympathize with the latter to some degree), so I guess the combination of protected and static must look bad^2 to those that belong to both groups.

x4u
  • 13,091
  • 5
  • 45
  • 55
3

Protected is used so that it can be used in subclasses. There is no logic in defining a protected static when using in the context of concrete classes as you can access the same variable is a static way.However the complier will give a warning to access the super class static variable in a static way.

1

Well, as most of the people have answered:

  • protected means - 'package-private + visibility to subclasses - the property/behaviour is INHERITED'
  • static means - 'the opposite of instance - it is a CLASS property/behaviour, i.e it is NOT INHERITED'

Therefore they are slightly contradictive and incompatible.

However, recently I came up to a use case where it might make sense to use these two together. Imagine that you want to create an abstract class which is a parent for immutable types and it has a bunch of properties which are common to the subtypes. To implement immutability properly and keep readability one might decide to use the Builder pattern.

package X;
public abstract class AbstractType {
    protected Object field1;
    protected Object field2;
    ...
    protected Object fieldN;

    protected static abstract class BaseBuilder<T extends BaseBuilder<T>> {
        private Object field1; // = some default value here
        private Object field2; // = some default value here
        ...
        private Object fieldN; // = some default value here

        public T field1(Object value) { this.field1 = value; return self();}
        public T field2(Object value) { this.field2 = value; return self();}
        ...
        public T fieldN(Object value) { this.fieldN = value; return self();}
        protected abstract T self(); // should always return this;
        public abstract AbstractType build();
    }

    private AbstractType(BaseBuilder<?> b) {
        this.field1 = b.field1;
        this.field2 = b.field2;
        ...
        this.fieldN = b.fieldN;
    }
}

And why protected static ? Because I want a non-abstract subtype of AbstactType which implements its own non-abstract Builder and is located outside package X to be able to access and reuse the BaseBuilder.

package Y;
public MyType1 extends AbstractType {
    private Object filedN1;

    public static class Builder extends AbstractType.BaseBuilder<Builder> {
        private Object fieldN1; // = some default value here

        public Builder fieldN1(Object value) { this.fieldN1 = value; return self();}
        @Override protected Builder self() { return this; }
        @Override public MyType build() { return new MyType(this); }
    }

    private MyType(Builder b) {
        super(b);
        this.fieldN1 = b.fieldN1;
    }
}

Of course we can make the BaseBuilder public but then we come to another contradictory statements:

  • We have a non-instantiatable class (abstract)
  • We provide a public builder for it

So in both cases with protected static and public builder of an abstract class we combine contradictory statements. It is a matter of personal preferences.

However, I still prefer the public builder of an abstract class because the protected static to me feels more unnatural in a OOD and OOP world !

egelev
  • 1,025
  • 2
  • 12
  • 24
0

There is nothing wrong with having protected static. One thing a lot of people are overlooking is that you may want to write test cases for static methods that you don't want to expose under normal circumstances. I've noticed this is particularly useful for writing tests for static method in utility classes.

Aelphaeis
  • 2,495
  • 3
  • 21
  • 41