Java 8 type inference static method call

ooxi

Since Java 7 it is allowed to use the diamond operator in order to avoid repeating the type argument. For example

List<String> myList = new ArrayList<String>();

becomes

List<String> myList = new ArrayList<>();

Why isn't the same notation allowed when using static method calls? For example when creating a SettableFuture I'd like to invoke

SettableFuture<String> myFuture = SettableFuture.<>create();

instead of

SettableFuture<String> myFuture = SettableFuture.<String>create();

I know it's possible to use the raw method call

SettableFuture<String> myFuture = SettableFuture.create();

but isn't this just the same as

List<String> myList = new ArrayList();

My question: Should I use the raw method call or repeat the type argument when calling generic static methods?

Javier Cabero

Java objects are created by means of the Constructor. Because this is the normal behavior, Java developers created the empty diamon as a sugar syntax feature.

A static method for object instanciation is not supposed to occur as frequent as the constructor. They are used for a lot of other stuff. Because of that, the syntax is not planned to detect if what you are trying to do is the creation of an object that needs a generic type.

EDIT:

I was wrong. There is syntax sugar also for types in static methods (leaving out the diamon) and create() is more common than I thought.

My answer: I would prefer to leave out the diamon as the type is already specified in the variable and this way you avoid information redundancy in your code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java 8 type inference with non-static access of static members

From Dev

Java Type Inference in Static Methods

From Dev

Java 8: generic type inference fails on method reference?

From Dev

Confusion over Java generic method type inference

From Dev

Confusion over Java generic method type inference

From Java

A peculiar feature of exception type inference in Java 8

From Dev

Java 8 and Generalized Target-Type Inference

From Dev

Java 8: Generic type inference improvements

From Dev

Reflection type inference on Java 8 Lambdas

From Dev

Very confused by Java 8 Comparator type inference

From Dev

Java 8: Generic type inference improvements

From Dev

Java type inference: reference is ambiguous in Java 8, but not Java 7

From Dev

Can a Java static initializer call a static method?

From Dev

Call a "static" method belonging to a generic type in scala

From Dev

Call static method from generic type

From Dev

Call static method from trait on generic type

From Dev

Call static method from generic type

From Dev

Java Bounded Generics: Type inference bug? (Method invocation, JLS 15.12.2.7)

From Dev

Why does Java generics type inference break in chained method calls?

From Dev

Why does the Java 8 generic type inference pick this overload?

From Dev

Java 8 Type Inference - How reduction is done for generic constructors?

From Dev

Java 8 method reference to static void method

From Dev

Type inference failed in the call to 'Join'

From Dev

Java type inference with erasure

From Dev

Type inference in java

From Dev

Call a non-static method in main static method in Java

From Dev

Java 8 filter with method call

From Dev

In java 8, why cannot call the interface static method that the current class is implementing

From Dev

Generic factory method and type inference

Related Related

  1. 1

    Java 8 type inference with non-static access of static members

  2. 2

    Java Type Inference in Static Methods

  3. 3

    Java 8: generic type inference fails on method reference?

  4. 4

    Confusion over Java generic method type inference

  5. 5

    Confusion over Java generic method type inference

  6. 6

    A peculiar feature of exception type inference in Java 8

  7. 7

    Java 8 and Generalized Target-Type Inference

  8. 8

    Java 8: Generic type inference improvements

  9. 9

    Reflection type inference on Java 8 Lambdas

  10. 10

    Very confused by Java 8 Comparator type inference

  11. 11

    Java 8: Generic type inference improvements

  12. 12

    Java type inference: reference is ambiguous in Java 8, but not Java 7

  13. 13

    Can a Java static initializer call a static method?

  14. 14

    Call a "static" method belonging to a generic type in scala

  15. 15

    Call static method from generic type

  16. 16

    Call static method from trait on generic type

  17. 17

    Call static method from generic type

  18. 18

    Java Bounded Generics: Type inference bug? (Method invocation, JLS 15.12.2.7)

  19. 19

    Why does Java generics type inference break in chained method calls?

  20. 20

    Why does the Java 8 generic type inference pick this overload?

  21. 21

    Java 8 Type Inference - How reduction is done for generic constructors?

  22. 22

    Java 8 method reference to static void method

  23. 23

    Type inference failed in the call to 'Join'

  24. 24

    Java type inference with erasure

  25. 25

    Type inference in java

  26. 26

    Call a non-static method in main static method in Java

  27. 27

    Java 8 filter with method call

  28. 28

    In java 8, why cannot call the interface static method that the current class is implementing

  29. 29

    Generic factory method and type inference

HotTag

Archive