#generics diamond syntax

40 messages · Page 1 of 1 (latest)

tepid sand
#

Hi guys I'm a little confused on generics. I understand what they are for and i see that instead of saying something like Integer thingToPrint you can just say T thingToPrint and T is the generic type. However i don't get how the diamond syntax is used when you put it at the top of a class.

topaz lichenBOT
#

This post has been reserved for your question.

Hey @tepid sand! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

tepid sand
#

Here i was watching a YouTube video and at 13:36 he adds a second generic type, V, but at that point he never had a type V in the top of a class header in diamond syntax, he only did that with T

#

So I'm confused when to put a generic type in diamond syntax at the top of a class header

brisk thistle
#

he has it in method declaration

#

private static <T, V> void shout(...)

tepid sand
brisk thistle
#

it's allowed to use generic methods without have whole class as generic

tepid sand
#

I'll check out this website

#

I think this is too hard for me

#

They write it so confusing

brisk thistle
#

you can declare generic variable at top of class, but also can declare generic variable at method declaration, so you can use it in scope of that method

tepid sand
#

So if im gonna use the generic everywhere in a class then i write <T> in the class header and then continue calling it T in the rest of the file? But if i want to only use a generic in one method, then i always call it <T> instead of just T whenever i wanna use it?

shadow aurora
#

Hi, generics little bit tricky but its easy to understand simple you just think in one class you have to use every data type but you write constant
example:
class Example {
Integer a;
}
like this but you want to use all datatypes what you do then you have to use generics
class Example<T> {
T a;
}
then when you create object that example class that time you have to pass the type
example :
Example<Integer> object = new Example<>();
Thats it

tepid sand
tepid sand
#

But in the video that never happened but it still worked

shadow aurora
#

class Example<T,V> like this ?

brisk thistle
shadow aurora
#

yeah that also be work

tepid sand
#

Like he might only have class Example <T> but then in a method he'll say T something and V something

tepid sand
#

So that's like the same as putting it in the class header except here we put it in just the method?

shadow aurora
#

method level also you can do generics brother

brisk thistle
tepid sand
#

So like declare generic in class header and you have 5 methods but only 1 uses generic is there anything wrong with that

tepid sand
topaz lichenBOT
# tepid sand Ok thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

tepid sand
#

Thanks for all help

brisk thistle
#

👍

brisk thistle
tepid sand