aspose file tools*
The moose likes Beginning Java and the fly likes getClass() vs .class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getClass() vs .class" Watch "getClass() vs .class" New topic
Author

getClass() vs .class

fahad siddiqui
Ranch Hand

Joined: Jun 14, 2006
Posts: 85
We can get the Class instance of a class by writing com.db.Manager.class or "com.db.Manager".getClass().
What is the difference between these?
Where is this .class defined and what is it? a variable? definitely not a method.

Please explain.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
(beginner question, not advanced)

Originally posted by fahad siddiqui:
We can get the Class instance of a class by writing com.db.Manager.class or "com.db.Manager".getClass().
What is the difference between these?
Where is this .class defined and what is it? a variable?


"com.db.Manager".getClass() is almost certainly not going to do what you want. It will give you the instance of java.lang.Class corresponding to the java.lang.String class, because "com.db.Manager" is a String.

The .class syntax is a special bit of syntax for efficiently getting the Class object for the class whose name precedes it (not in quotes) in the source code. It can only be used when the exact class is known at compile time.

The getClass() method is defined in java.lang.Object and hence can be called on any object reference. It gets the Class object associated with the run-time type of the object to which the reference points.

The getClass() method is not really analogous to .class at all. Closer is Class.forName(String), which gets a Class object for the class named by the String.

In situations where either could be used, use .class , as it is more efficient.
[ August 15, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
fahad siddiqui
Ranch Hand

Joined: Jun 14, 2006
Posts: 85
Where can i find the .class implements or something in the source code of java? in which class? in the rt.jar ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

The ".class" notation is a feature of the Java language itself. It's not something in the standard Java library, so you will not find the source code for it in src.zip (which contains the sources of rt.jar).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jesper Young:
The ".class" notation is a feature of the Java language itself.


More specifically, it's called a "class literal" - just as 2.0 is a double literal.

See http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#251530
[ August 16, 2007: Message edited by: Ilja Preuss ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getClass() vs .class