This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Class class

 
Ranch Hand
Posts: 46
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why first one doesn't work?
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question. Even i want to know that why its not working. i tried with String class. for that its working but for ArrayList its not:(
 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I want to show following excerpt from "core java volume -1 fundamentals" ..

Type Variables Are Not Valid in Static Contexts of Generic Classes
You cannot reference type variables in static fields or methods. For example, the follow-
ing clever idea won’t work:

If this could be done, then a program could declare a Singleton<Random> to share a random
number generator and a Singleton<JFileChooser> to share a file chooser dialog. But it can’t
work. After type erasure there is only one Singleton class, and only one singleInstance field.
For that reason, static fields and methods with type variables are simply outlawed.



So in our case

so you need to make it ..


In the second case , instance method is dynamically invoked to return the Class type object,
so it doesnt't generate any error..

HTH

Still i want more experts to have a look at this query..
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is due to the generic syntax. Generic types don't mean anything at runtime, so ArrayList<Integer>.class, ArrayList<String>.class and ArrayList.class all mean the same at runtime as the actual class is ArrayList and the generic type is just for compile time type safety...
 
reply
    Bookmark Topic Watch Topic
  • New Topic