0

EDIT : The linked duplicate DOES NOT answer this question as it is about HOW it is accessible. But my question is when should such a design be used or any examples of such a design.

NOTE : Anyone looking for an answer to this question can scroll down in the comment section to see Thomas's comments in the end related to java.lang.CharacterData.


Some of the questions with a similar title actually discuss the usage of static variables with the same name in child and parent. example: Java | static vars in parent and child class | accessing the child var value from parent class

But my question is regarding the purpose of accessing child's static field in parent class.

class Child extends Parent {
    static String someString = "test";
}

class Parent {
    void someMethod() {
        System.out.println(Child.someString);
    }
}

Isn't this a bad design? Ideally, if someString is required in Parent, then it should be a field in Parent instead of Child.

Are there any use-cases/examples when this approach could be used?

Gautham M
  • 4,816
  • 3
  • 15
  • 37
  • 3
    I agree with you, this **is** bad design. But why should java specifically not allow accessing a public static field of a subclass (when it allows accessing public static fields of all other classes?) – Thomas Kläger May 14 '21 at 16:06
  • False premise. Java doesn't let you do that necessarily. Default access is package private (in this case, anyway; the defaults vary a bit with context), which these 2 classes presumably are. The inheritance aspect of this is not relevant. – Michael May 14 '21 at 16:07
  • @ThomasKläger but why should I be declaring a static variable in the child class if it is to be used in the parent? – Gautham M May 14 '21 at 16:08
  • 3
    If you don't want it to be used outside of the class in which it's declared, make it private. – Michael May 14 '21 at 16:09
  • Maybe the child is the primary implementation of a parent. That's a pretty common thing. – Louis Wasserman May 14 '21 at 16:10
  • @ThomasKläger "*But why should java specifically not allow accessing a **public** static field of a subclass*". It's not public. – Michael May 14 '21 at 16:12
  • @Michael Louis's comment might be one use case. `Child` and `Parent` are in the same package, but if I do not want `someString` to be accessible to other subclasses (which are in a different package) this could be used right? But still it would be the same as declaring a default field in `Parent` ? – Gautham M May 14 '21 at 16:18
  • There is a table in the linked duplicate which shows the access for subclasses, both in the same package and different packages. – Michael May 14 '21 at 16:19
  • @Michael The linked duplicate is about "how" the field is accessible. My question is "when should" accessing a child's static field in parent be used. – Gautham M May 14 '21 at 16:32
  • That's a matter of opinion. Opinion-based questions are off-topic. – Michael May 14 '21 at 16:34
  • @Michael yes it is kind of opinion based but not a duplicate :-) – Gautham M May 14 '21 at 16:45
  • 2
    I agree that the duplicate doesn't answer the question, but if it's off topic anyway then there's no point reopening it. – Henry Twist May 14 '21 at 16:49
  • 1
    I have finally an example where this pattern is used (it's even in the JDK): the class `java.lang.CharacterDataLatin1` extends `java.lang.CharacterData` and has a field `static final CharacterDataLatin1 instance = new CharacterDataLatin1();` that is used from [`java.lang.CharacterData.of()`](https://github.com/openjdk/jdk11u/blob/master/src/java.base/share/classes/java/lang/CharacterData.java#L79). – Thomas Kläger May 14 '21 at 18:13
  • Here the parent class `CharacterData` delegates some of it's work to subclasses that are only loaded if they are needed. – Thomas Kläger May 14 '21 at 18:14
  • @ThomasKläger Great! Looks like this pattern is good to use when singleton child classes are involved. – Gautham M May 15 '21 at 04:39
  • 1
    I have to change my first comment: this can be bad design. But it can also be useful for example if a parent class contains a factory method that returns singleton-instances of one of several subclasses. And then: why should java specifically enforce stricter access rules from a parent class to a subclass's fields than from other classes to those same fields? – Thomas Kläger May 15 '21 at 06:54
  • @ThomasKläger My initial phrasing of the question `"Why does Java allow..."` was actually confusing, my intention was to know the use-cases of such a pattern rather than asking "why does this work". I have edited the title. Hope it is more clear now. – Gautham M May 15 '21 at 07:08

1 Answers1

0

Based on Thomas's comment:

This pattern could be used to return a specific implementation from a method in the parent class. i.e it is useful when a parent class contains a static factory method that returns singleton-instance of one of several subclasses.

An example available within the JDK is java.lang.CharacterData. This is an abstract class which is extended by 8 other classes (CharacterData00, CharacterData01, ...) All these implementing classes are non-public and singleton and hence contains a static final instance variable. Since these implementations are non-public (default access), it is not supposed to be accessed directly. So, the CharacterData contains a static factory method of, which returns the appropriate implementation.

Gautham M
  • 4,816
  • 3
  • 15
  • 37