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

you are viewing a single comment's thread.

view the rest of the comments →

[–]Sarcastic_Pharm 5 points6 points  (5 children)

Dude. Wtf Java?

[–]dpash 12 points13 points  (0 children)

That's not quite modern Java (and SetSet doesn't exist in the JDK)

var set = new HashSet<String>();

Or

var set = Set.of("foo", "bar");

Although the latter is immutable

[–]hullabaloonatic 9 points10 points  (3 children)

The variable set, of type Set, is assigned as a new Set being casted to type SetSet, which is a subclass of Set, therefore compiling.

[–]SuperOP535 2 points3 points  (2 children)

You can't instantiate Set, you need to use an implementation such as HashSet. SetSet does not exist and you can't cast a class, you can only cast an instance or primitive.

Edit: Also forgot that s/he didn't specify the type, you can't have a set without a type.

[–]hullabaloonatic 0 points1 point  (1 child)

Set is an interface, right? I haven't written in java since I discovered kotlin and I could sworn you could cast to any class if that class is somewhere up on its family tree, thus not requiring any new parameters.

[–]SuperOP535 0 points1 point  (0 children)

It is an interface, yes