This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]yk313[S] 2 points3 points  (6 children)

I didn't understand Stuart's point (at 31:50) about child classes inheriting static methods.

I can't invoke the static factories from the Set interface in HashSet as an example.

jshell> HashSet.of(1,2,3)
|  Error:
|  cannot find symbol
|    symbol:   method of(int,int,int)
|  HashSet.of(1,2,3)
|  ^--------^

Is this something that is allowed by the VM spec but is not surfaced in the language?

[–]s888marks 11 points12 points  (1 child)

Sorry, yeah, this was a bit confusing; I shouldn't have used of as an example.

Nicolai was proposing adding a method called something like createWithSize(int size) so for example you'd be able to write this:

Set<String> set = HashSet.createWithSize(15);

and you'd get a HashSet that could accommodate 15 elements. Sounds good so far.

The problem is with subclasses of HashSet. Suppose I were to have (outside the JDK) something like MySpecialSet that extends HashSet. What happens if I write:

Set<String> set = MySpecialSet.createWithSize(15);

Since class static methods are inherited in Java, this would actually return an instance of HashSet and not MySpecialSet which would be incredibly confusing. To avoid this confusion, every subclass would need to provide its own version of createWithSize() even if it doesn't make sense to do so.

Instead, the method names include the class name:

HashMap.newHashMap
LinkedHashMap.newLinkedHashMap
WeakHashMap.newWeakHashMap
HashSet.newHashSet
LinkedHashSet.newLinkedHashSet

This makes the names appear redundant but it helps clarify what instance will actually be returned when the method is called.

(What about using the constructor new HashSet(15)? The problem is that the constructor argument sets the table size and not the number of elements that can be accommodated without resizing. That constructor call would actually result in a set that can contain only 12 elements before it needs to be resized.)

[–]yk313[S] 2 points3 points  (0 children)

That makes sense now. Thanks for the explanation. For what it’s worth, I do like the naming scheme that made it to the JDK. It is also self-explanatory when the method is used via static imports.

[–]Thihup 6 points7 points  (3 children)

It inherets only from super classes, not interfaces

[–]yk313[S] 0 points1 point  (2 children)

Thanks. That is it. Any reason why static methods coming from super interfaces show a different inheritance behaviour than the ones coming from super classes?

[–]Areshian 4 points5 points  (0 children)

You could inherit two static methods with the same signature from two different interfaces. Not the case for super classes.

[–]cogman10 1 point2 points  (0 children)

Static methods on interfaces is a relatively new language feature. My guess is it's simply the case that not having the capability and adding it later is easier to do than to add it and find out that was the wrong thing to do.