• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
^
TestRental.java:7: cannot find symbol
symbol : class ArrayList
location: class Rental
private ArrayList<T> al;
^
TestRental.java:7: cannot find symbol
symbol : class T
location: class Rental
private ArrayList<T> al;
^
TestRental.java:9: cannot find symbol
symbol : class ArrayList
location: class Rental
Rental(ArrayList<T> al){
^
TestRental.java:9: cannot find symbol
symbol : class T
location: class Rental
Rental(ArrayList<T> al){
^
TestRental.java:12: cannot find symbol
symbol : class T
location: class Rental
public T getRental(){
^
TestRental.java:15: cannot find symbol
symbol : class T
location: class Rental
public void returnRental(T t){
^
TestRental.java:23: cannot find symbol
symbol : class ArrayList
location: class RentalTest
ArrayList<Car> al=new ArrayList<Car>();
^
TestRental.java:23: cannot find symbol
symbol : class ArrayList
location: class RentalTest
ArrayList<Car> al=new ArrayList<Car>();
^
9 errors
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;
^


Compare your import statement with the package to which ArrayList belongs http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:What's problem with this code ?


Which package is ArrayList in?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew and Jaikiran but still

F:\OCJP\Chapter7\Generics>javac TestRental.java
TestRental.java:7: cannot find symbol
symbol : class T
location: class Rental
private ArrayList<T> al;
^
TestRental.java:9: cannot find symbol
symbol : class T
location: class Rental
Rental(ArrayList<T> al){
^
TestRental.java:12: cannot find symbol
symbol : class T
location: class Rental
public T getRental(){
^
TestRental.java:15: cannot find symbol
symbol : class T
location: class Rental
public void returnRental(T t){
^
4 errors
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).



Do you mean if i want declare some generic methods then is it mandatory to declare class also generic.I mean, i just want some generic methods in class but not generic class then ?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Please give an example.thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of what, a generic method?

Quite a few of the methods in java.util.Collections are examples. For instance:
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to do so but

F:\OCJP\Chapter7\Generics>javac GenericTest.java
GenericTest.java:16: cannot find symbol
symbol : method add(Car)
location: class java.util.ArrayList<T>
al.add(new Car("suv"));
^
1 error
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you actually trying to do? It looks like you're trying to use a generic method in a situation where there's no point in it being generic.

That doesn't compile because of line 16.

al is a variable of type ArrayList<T>. T might be anything as far as that method goes, so it can't let you add a Car object to it.

Generic methods are useful for methods that work the same way for lots of objects. The examples in Collections are like that: you can sort a list of any type of object, shuffle a list of any type of object, have an unmodifiable view of any sort of object, etc. Your returnRental method only makes sense if it's passed a list of Car objects, so there's no reason for it to be generic.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew Brown
 
reply
    Bookmark Topic Watch Topic
  • New Topic