• 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

What is meant by .class ?

 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In methods that require a parameter of type java.lang.class, I've seen the following syntax...
So is saying:
java.lang.String.class
kind of like a shortcut for saying:
Class.forName("java.lang.String")

?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. It's called a class literal. Looks like a static field or something but isn't (as you can determine via reflection if so inclined). The compiler interprets it as a direct reference to a particular Class object, the same way "foo" is a direct reference to a String object with contents f-o-o, without any visible constructor or method call. Interestingly, you even have Class objects for
int.class
void.class
These are useful for things like specifying the return type of a method when using reflection (which wants to use Class objects for everything, even things that aren't anything.)
I'm mildly disappointed to see there's no null.class, even though I can't (yet) imagine what we could possibly use it for. I'll think of something...
[ May 20, 2003: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is reflection?
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trail: The Reflection API
Using Java Reflection
Take an in-depth look at the Java Reflection API
 
You got style baby! More than this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic