Sergey,
It looks like, at this moment, your head is messes up very well where it comes to generics. Otherwise you would not write this
T1
. This is a type which is
not defined anywhere. In contrast,
T
is a generic type. It actually means "that very
complete type which will ultimately be substituted instead of the generic type parameter". If you meant
T
instead
T1
, you should have written so, if this is meant to be another type, you should have added it as a generic parameter in class
MyClass<T, T1> { /* ... */ }
.
Now, using the variable if the same class is a very usual thing; please see Solution 1. However, you need a
big warning about what you tried to do.
This is allowed by syntax, but
will cause infinite recursion:
class MyClass<t> {
MyClass<t> myInstance = new MyClass<t>();
}
</t></t></t>
And it has nothing to do with generics; you would have exact same problem without then:
class MyClass {
MyClass myInstance = new MyClass();
}
Do I even have to explain why? If it's not clear, just think just a bit.
—SA