• Post Reply Bookmark Topic Watch Topic
  • New Topic

dot class?  RSS feed

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, today I saw something that I'm not quite familiar with. I am calling a method that takes a "java.lang.Class" as a parameter. I was trying to instantiate a class called "SimpleJavaAction" to send as a parameter. Instead I was told by a coworker to just send SimpleJavaAction.class as the parameter.

I just want to know what exactly I am doing by adding .class to the end of the class that I wrote. It looks like I must have a static variable on that class called class? But I can't find it in the Java API documentation? Can someone help explain this to me?

Ben
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to do anything to make this work - it's a shortcut that the compiler/JVM provide. Functionally it's equivalent to calling the getClass method on an object of that class.
 
Benjamin Hundley
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I know it works. I guess I just wanted to understand where this static variable called class was. Sounds like you're saying it's not a variable anywhere. It's just something that was added to the compiler for convenience...
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its called a "class literal", you can find out (a little) more about it here. Scroll down to the section on literals.
 
Benjamin Hundley
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!