• Post Reply Bookmark Topic Watch Topic
  • New Topic

.class  RSS feed

 
Justin Bleach
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what a class is etc. What I haven't discovered, while studying for my SCJP is what this does:

Class target = someObject.class;

What does the .class do on the "someObject" instance? Or is it even an instance since there is no "new" reserved word.

This has been bugging me for a while now.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Justin,

Welcome to JavaRanch!

"someClass" in this case isn't an instance; it can't be a variable name. It has to be the name of a class. "ClassName.class" is a reference to a special object, an instance of the special class java.lang.Class, which represents ClassName in the Java Virtual Machine. There's one instance of java.lang.Class for every class loaded by the JVM, and it serves various purposes littered throughout the Java APIs; most of these purposes are fairly obscure. But sometimes you need that object for a class, and there are several ways to get it:

 
Justin Bleach
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to wrap my head around why this is even necessary after reading the API description for java.lang.Class.

Also am I understanding that it's just a quick and dirty way of getting another instance?

Still kind of confused. I thought you always knew what objects you had and what they were anyway. How does having a reference to a JVM level representation of a Class really help? I know you said the reasons were mostly obscure but I have to know for my own sanity!
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you need a reference to the class java.lang.String. Now you can do this by creating an instance, then calling getClass on it. Or you can just use java.lang.String.class (or String.class for short) and spare yourself an object you don't need anyway.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin --

The least obscure use of Class objects is as the synchronization monitor for static methods. If you know what that means, awesome. If you don't, then I'm not going to explain it now -- all in due time.

Here's something a little in use, but much cooler and easier to envision. It's definitely the most interesting use for Class objects.

OK, so, let's say you've written an applet. This involves writing your own class (let's call it HelloApplet) which is a subclass of java.applet.Applet. Then you have an HTML page like



Now, imagine the fellow who has to write the Java plugin for the browser. Somehow, he has to take that String "HelloApplet" and create an instance of your applet. All he knows about it that it's a subclass of java.applet.Applet. Note that the name "HelloApplet" is not available when he's writing the plugin -- it's something that his program finds out about only by reading the HTML page. How can he turn that name into an actual object that he can display? You might want to sit and think about how you'd do this for a minute (hint: look up at my previous post...)

It's easy, actually. You ask the JVM for the Class object for that class using the static "forName" method; then you ask that Class object to create a instance of the class it represents using the "newInstance()" method. So...



This kind of magic is what makes applets, servlets, JSPs, and many other Java frameworks work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!