• 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

error creating my own Generic Class

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have created my own generic class and another class to test the generic class,


Below is the test code for above class



The eclipse error on executing same says car cannot be resolved to a type. This is same code as given by kathy sierra in SCJP 1.6

Please advice where am i going wrong
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henna. Welcome to the Ranch!

Have you added a Car class anywhere? That's not a part of the standard library, so you need to have defined it somewhere.

 
Henna Singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Matthew. But why do i need to create a car class, i created a generic class for the same. i am trying to test the generic class i created
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i created a generic class for the same


You misunderstand how generics work. What you created is a class that can take any other class as parameter. But any specific class you use in your code needs to be defined. Or if you just want to test generics and don't care about cars in particular, you could use an existing class such as "Object" instead of "Car".

You may also want to work through the generics tutorial: http://docs.oracle.com/javase/tutorial/extra/generics/index.html
 
Henna Singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Ulf, i am just trying to test the code as given in SCJP 6 by Kathy Sierra. This is how she defined creating my own generic class and then test class to test the code created. i have just copied the code from book, but its throwing an error
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not familiar with the book, but as long as you don't define a Car class, this is not going to work.
 
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
A generic class uses a placeholder for a real class that will be supplied later. Your generic class there is fine. But when it is instantiated, you are giving it a real class. In this example the book is assuming there is a class called Car that already exists and is using that to demonstrate using the generic class.

So to get it to compile, just add a Car class.
 
Henna Singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Matthew and Ulf. I got the concept. Thank you soo much.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic