Tell the difficulties that i am difficult.
saloni jhanwar wrote:What's problem with this code ?
F:\OCJP\Chapter7\Generics>javac TestRental.java
TestRental.java:1: cannot find symbol
symbol : class ArrayList
location: package java
import java.ArrayList;
^
saloni jhanwar wrote:What's problem with this code ?
Tell the difficulties that i am difficult.
Matthew Brown wrote:There's nothing magical about the letter T. You're using it as if it was a class name, so the compiler is treating it as such, and it can't find a class called that.
It's a similar error to using a variable without declaring it. If you want to use a generic type parameter you need to declare it. For a generic class, that means declaring it like this:
Once you've done that, you can then refer to T within the class, and the compiler will know what you mean.
The other change you'll need to make is when you create the Rental object you need to provide the specific type that's going to be used for that instance (just like you do with the ArrayList on line 23).
Matthew Brown wrote:You can declare generic methods separately from generic classes. In that case there's a different syntax - the type declaration is part of the method specification. But the example you had needs a generic class: you've got a private instance variable using the generic type.