![mynotes java mynotes java](http://java.eism.net/_/rsrc/1468855292177/config/customLogo.gif)
Then what am I supposed to do at this line of code: String str = unknownWrapper.getRef() IĪm going to show you a compile error so that you know that is not right. Wrapper unknownWrapper = new Wrapper(1234). What if you created a wrapper class with something like this: Hey you never said that the getRef method is supposed to return just String. So if you use something like String str = unknownWrapper.getRef(), the compiler is going to Return type of the methods in this class are going to be. Now you are telling the compiler that you don't know what the type of argument or the But then, you can also instantiate the class as Wrapper. Make sure that there are no surprises during run-time. Replace all occurrences of T with String) the compiler can then kick-in and perform all the necessary checks it needs to do to Once you have made it clear that T is in fact String (by instantiating the Wrapper class with Wrapper, you areĮssentially telling the compiler that you intend to use the Wrapper class as a Wrapper class. The getRef and the setRef methods in the class make use of the T parameter either as argument to a function, or as a return type. So the compiler will know that you intend to use this class with a Wrapper class, you will do it like this for example: Wrapper. I think what you basically have to wrap your head around is this: The class Wrapper is defined as Wrapper. Results at runtime, it will allow the operation on the wildcard generic type reference. As long as the compiler is satisfied that the operation will not produce any surprising Purpose of using generics is to have compile-time type-safety in Java programs. However, there is a simple rule of thumb to remember. There is a complicated list of rules as to what a wildcard generic type reference can do with the object. In this case, box would just be a box of Objects. Raw Types: Using a generic class or interface without using type arguments is a raw type. An invocation of a generic type is known as a parameterized type.
![mynotes java mynotes java](https://4.bp.blogspot.com/-b_UXowGsaoo/VFom-wv1rXI/AAAAAAAAA6c/DFTuk9XkHzo/s1600/jcp-sec.png)
![mynotes java mynotes java](https://2.bp.blogspot.com/-wQHxFOTLFZU/VFom3n80jBI/AAAAAAAAA6U/uqAl-QLHlmk/s1600/java-reopen-jcp.png)
Type argument, Integer in this case, to the Box class itself. Similar to how you would pass an argument to a method invocation, you pass a Generic Type Invocation: To reference the generic Box class from within your code, you must perform a generic type invocation, which This lesson observes this definition when using these terms. Therefore, the T in Foo is a type parameter and When coding, we provide type arguments in order to create a parameterized type. Type Parameter and Type Argument: Many developers use the terms "type parameter" and "type argument" interchangeably, but these terms are not Type parameter naming conventions: E - Element, K - Key, N - Number, T - Type, V - Value, S,U,V etc.