File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object.class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object.class" Watch "Object.class" New topic
Author

Object.class

Ependi Jusdi
Greenhorn

Joined: Jun 04, 2002
Posts: 7
taken from Sun Guoqiao's Mock Exam 3 No.19.
i cannot find information about this in JLS or Java API.
Problem: "Object.class.something" will returns an instance of class Class.
Can anyone help me to explain more about this?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I don't follow what you're asking for. By getting the class member of a Class, in this case, the T019 Class, you get an instance of a Class object. As every class in Java extends Object and Object has a wait method, the Class object that you get also has a wait method (through inheritence).
I don't know if this answers your question but, then again, I don't know what your question is.
Corey


SCJP Tipline, etc.
Ependi Jusdi
Greenhorn

Joined: Jun 04, 2002
Posts: 7
my doubt is: this statement is not a method's call, or not a variable declaration..., i never see this kind of statement before. so what kind of declaration is this?
another thing is "T019.class" will return instance of Class which is T019 Class, so my understanding is we can do something like below, but i am wrong, can u explain more about this.

<CODE>
public abstract class T019{
static int x=10;
public synchronized static void main(String args[]){
try{
System.out.println(T019.class.x); //compile time error
T019.class.wait(); //3
}catch(InterruptedException ie){ }
}
}
</CODE>
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182

why they made this ".class" if the class Object already have a "getClass()" method? do I always get the same result with each of them?
Francisco


Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
OK, try to follow this sentence:
When you access the class member of a Class, you get an instance of a Class class that represents that class. :roll:
So, what the heck does that mean?
First of all, be aware that there is a class in Java called java.lang.Class. This class is a representation of another class andf you can untilize an instance of the Class class to do things like get the names of the fields of the Class that is represents, or the names of the methods that the represented class contains. You can even use the Class class to create an instance of the class that it represents. Note, however, that an instance of the Class class is not the same as an instance of the class that it represents. Rather the instance of the Class class "describes" the class that is represents. Be sure to check out the API Spec of the Class class.
So, if you use the following code:

The variable c could tell you about the class T019. It could tell you that T019 has a member named x, for example. You could even use T019 to create an instance of the T019 class, but the variable c references a Class object, not a T019 object. Therefore, the following code is illegal:

The Class class has no member named x.
I hope that helps,
Corey
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Francisco A Guimaraes:

why they made this ".class" if the class Object already have a "getClass()" method? do I always get the same result with each of them?

Each of these approaches (accessing the class member of the getClass method) will return an instance of the Class class which represents that designated class.
However, notice that the class member is static and the getClass method is not. Therefore, the getClass method must be invoked on an object.
Also, one important point - from the API Spec for the getClass method:

Returns the runtime class of an object.

Look at this code:

This method can be useful in getting the actual class represented at runtime, which is something that the class member can not do because it is static.
I hope that helps,
Corey
[/code]
Ependi Jusdi
Greenhorn

Joined: Jun 04, 2002
Posts: 7
thank's Corey, i see the light now, btw, just for curiousity, where can i find this related matter in JLS/API?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4714
    
    7

See the JLS 15.8.2: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#250782
The construct Type.class is a called a "class literal"
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
I looked in the API and it doesn�t say that Object has a static member called "class", why is this?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4714
    
    7

Again, refer to the JLS section I mentioned before. It's a literal, same as null or true and is evaluated at compile time. The following expressions are also valid:
void.class
int.class
char[].class
Object[].class
---
Note also that you can't use '.class' with a variable. It must go with a type name (class, interface, array, or primitive type)
Object o = new Integer(0);
System.out.println(o.getClass()); // OK: java.lang.Integer
System.out.println(o.class); // ILLEGAL: compile error!
Junilu
[ June 05, 2002: Message edited by: Junilu Lacar ]
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
thanks
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Junilu Lacar:
It's a literal, same as null or true...

I should have pointed out that class is a literal and not a true member. It is just accessed as if it were one. Thanks, Junilu.
Corey
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
but what�s the meaning of int.class? Is there a class for int, char and etc? shouldn�t int.class return the Class object that represents the int wrapper class?
Francisco
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4714
    
    7

It's usually used when programming to the Reflection API. The Class.getConstructor() method returns an instance of java.lang.reflect.Constructor if a constructor exists that takes the specified parameter types, specified by an array of Class. If you want to get a constructor that takes a primitive type, say an int, what Class are you going to pass? Answer: int.class
As I said, you normally use these facilities when dealing with the Reflection API. The flexibility comes at the (high) cost of performance.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

shouldn�t int.class return the Class object that represents the int wrapper class?

No it shouldn't, the class object representing the wrapper is Integer.class. You can compare them with == to see they are not the same.
Class literals are a way to access the instances of the class Class. The JVM creates an instance of the class Class for each different type that loads in the same class loader (roughly speaking). When a class is loaded a lot of information, such as its name, static methods, and static variables; is stored in a static area called the method area. The class Class is a way for the programmer to access part of this information.
Also as Junilu said, the class Class is used for reflection. That is why Void.TYPE and Integer.TYPE exist. Though void and int are not types, they have a corresponding class instance for representing them when needed in reflection.
BTW. Integer.TYPE and int.class points the same class object.
[ June 06, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object.class