#generics diamond syntax
40 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @tepid sand! Please use
/closeor theClose Postbutton 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.
Full tutorial for using Generics in Java!
☕ Complete Java course:
https://codingwithjohn.thinkific.com/courses/java-for-beginners
Generics in Java can be very confusing for beginner Java learners. Generics are one of the coolest features in Java, but the syntax may not make a lot of sense at first.
In this beginner's Java tutorial video, we'...
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
I see that but why doesn't he need to put it on the class header
it's allowed to use generic methods without have whole class as generic
This Java tutorial describes generics, full screen mode API, and Java certification related resources
I don't understand
I'll check out this website
I think this is too hard for me
They write it so confusing
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
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?
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
That makes sense
But also in the video i linked at the time he puts a generic T but also a V and he never specified a V in the class header
Here we have a <T> on the header
But in the video that never happened but it still worked
class Example<T,V> like this ?
class Tests {
public static <T, V> void doSomething(T t, V v) { ... }
}```it works, because you declared generic variables for that particular method. No need to declare them at top of class.
yeah that also be work
I meant like he doesn't do that
Like he might only have class Example <T> but then in a method he'll say T something and V something
Is declaring generic variables for a method is that right after it says static
So that's like the same as putting it in the class header except here we put it in just the method?
method level also you can do generics brother
same as, only that visibility scope is different. If you declare for class, so it will be visible in all class, if you declare it at method declaratiion - it will be visible only for that method
Is there anything wrong with declaring it in the class and then using that generic type only for one method, even if there are a bunch of other methods that take in some other specific types
So like declare generic in class header and you have 5 methods but only 1 uses generic is there anything wrong with that
nothing wrong, imo
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.
Thanks for all help
👍
Could you close the post please?
Sorry didn't notice it didnt close