This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
Joined: Jun 14, 2006
Where can i find the .class implements or something in the source code of java? in which class? in the rt.jar ?
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).
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