4

In our code base, I have seen the following code snippet and I could not figure out what it is (that is why, I could not start searching for more information). The code snippet is as follows;

TypedId.< UserGroupsVO > valueOf( 1000L )

For more clarification, the definition of the TypedId class is as follows;

public final class TypedId< T > implements Serializable, Comparable< TypedId< T >>

Under which topic can one learn more about this syntax and what it means?

EDIT

After comments, I need to clarify my question. In my question, I did not mean the Generics. The part, i did not understand is the point between TypedId with <...> and there is a space between <...> with valueOf.

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
Bernhard Colby
  • 311
  • 5
  • 17
  • 2
  • 1
    ^^ See https://docs.oracle.com/javase/tutorial/java/generics/types.html – Fildor Mar 02 '17 at 12:19
  • I do not mean the angle brackets and Generics. I did not understand the point after TypedID and there is nothing between the first part with valueOf. – Bernhard Colby Mar 02 '17 at 12:21
  • Unclear if you didn't understand what the angle brackets means or how is used this code snippet. In the second case you also should post the context where your code snipped is. – freedev Mar 02 '17 at 12:21
  • 1
    Oh, I see. It is a call to the method `valueOf` in `TypeId`. I guess it is a static method. Since TypeId is a generic class, the method has to be called with a Type. – Fildor Mar 02 '17 at 12:24
  • What does the definition of `valueOf` look like? – khelwood Mar 02 '17 at 12:25
  • 2
    See http://stackoverflow.com/questions/5297978/calling-static-generic-methods – khelwood Mar 02 '17 at 12:25
  • @khelwood the definition of the valueOf is public static < T >TypedId< T > valueOf( long aValue ) – Bernhard Colby Mar 02 '17 at 12:26
  • @BernhardColby So `valueOf` has a generic type parameter `` . That's what `` is specifying. – khelwood Mar 02 '17 at 12:27
  • 1
    The spaces probably has been inserted for "readability", where my personal opinion is that spaces make it _less_ readable. There could also be some company code convention that regulates when and where to use spaces. – Fildor Mar 02 '17 at 12:28
  • Then it is just a static method call with type definition inbetween. – Bernhard Colby Mar 02 '17 at 12:29
  • And the space is just a... whitespace. I mean there's no sintactic rule that forces the following identifier (method, member, etc) to come straight after the dot - I can't link any spec though. – watery Mar 02 '17 at 12:29
  • 1
    Just for everyone saying "It's because `TypedId` is a generic class", it's not. It is because `TypedId.valueOf` is a **generic method**. – khelwood Mar 02 '17 at 12:30
  • @khelwood Thanks, you're right. When I realized my mistake, I already couldn't edit the comment anymore. Of course a generic class can have static methods that are not generic and don't need that. – Fildor Mar 02 '17 at 12:31
  • @khelwood Would you consider collecting your comments as an answer. Thereby, I can accept it as solution? :) – Bernhard Colby Mar 02 '17 at 12:34
  • @AlexK. Please, see the editted version of the question and consider removing your duplicate notification. – Bernhard Colby Mar 02 '17 at 13:15

2 Answers2

4

The static method valueOf

public static <T> TypedId<T> valueOf(long aValue) ...
              ^^^

has a generic type parameter T. That means that when you call TypedId.valueOf(...), you can also specify what the generic type T is. This is done via

TypedId.<UserGroupsVO>valueOf(...)
        ^^^^^^^^^^^^^^

The spaces in your code TypedId.< UserGroupsVO >valueOf are just cosmetic: they don't affect the meaning.


Note that it is not because TypedId is a generic class. A generic class can have a non-generic static method; and a non-generic class can have a generic static method.

For instance, here is a non-generic class with a generic static method.

class Foo {
     public static <T> List<T> makeList() ...
}
...
Foo.<String>makeList(); // call with a generic type parameter

And here is a generic class with a non-generic static method.

class Bar<T> {
     public static void doThing() ...
}
...
Bar.doThing(); // call without a generic type parameter
khelwood
  • 55,782
  • 14
  • 81
  • 108
2

TypedId< T > is a generic class that define a type T.

In this class you probably have a method TypeId<T> valueOf which use the type parameter T. This method is called a generic method.

This way you avoid to use Bounded wildcards which are more restrictive. Ex : <? extends E> is a bounded wildcards.

When you call TypedId.< UserGroupsVO > valueOf( 1000L ), you define explicitly that type argument T will be a UserGroupsVO

JLS Ref :

alain.janinm
  • 19,951
  • 10
  • 65
  • 112