• 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

creating restricted object type to Generic classes

 
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i wanted to create a class which only be created of type Integer so i put <Integer> after Test. but on compiling this class i am getting error "can not make a static reference to a non static type Integer" ISit not the correct way to create an object of the class which takes only Integer?.if it is not then how?

My Second Question is -- Is it possible to create a class using Generics whose objects can be only of two types say "Integer" and "String".
May be this is very silly question to put ..but i am just trying to understand Generics better.



[ November 24, 2008: Message edited by: Tanu Gulati ]
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tanu Gulati:
Hi,
i wanted to create a class which only be created of type Integer so i put <Integer> after Test. but on compiling this class i am getting error "can not make a static reference to a non static type Integer" ISit not the correct way to create an object of the class which takes only Integer?.if it is not then how?


If the class's "generics" are only limited to Integer, then why use generics at all?? It's like Henry Ford's famous quote:

Any customer can have a car painted any colour that he wants so long as it is black.



If you still need this though, you can make use of the fact that Integer is final:

There is only one class for which this will match: Integer.

My Second Question is -- Is it possible to create a class using Generics whose objects can be only of two types say "Integer" and "String".


Unfortunately, it is not. You can specify AND using &

OR is not possible as far as I know.
[ November 24, 2008: Message edited by: Rob Prime ]
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mmmm...i started understanding Generics ....a big thanks to you Rob
 
reply
    Bookmark Topic Watch Topic
  • New Topic