wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics  doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics  doubt" Watch "Generics  doubt" New topic
Author

Generics doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Tell the difficulties that i am difficult.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9327
    
109

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

[My Blog] [JavaRanch Journal]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Which package is ArrayList in?
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Feb 09, 2012
Posts: 583

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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Feb 09, 2012
Posts: 583

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

Joined: Apr 06, 2010
Posts: 4240
    
    7

Of what, a generic method?

Quite a few of the methods in java.util.Collections are examples. For instance:
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Feb 09, 2012
Posts: 583

Thanks Matthew Brown
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics doubt
 
Similar Threads
sample code in Chapter 7 of K&B SCJP 6.0
Generics...
generics... page 598
Doubt on generics once again
Generics <?>