| Author |
using getclass on references
|
ronnir paterl
Greenhorn
Joined: Oct 18, 2007
Posts: 11
|
|
Hi, Im facing this wierd problem while using getClass method on different references e.g intarray.getClass() gives me "Class [I" where intarray is a reference to int[] Objarray.getClass() gives me "Class [Ljava.lang.Object;" where Objarray is a reference to Object[] .. also why does it have ";" in the end myclass.getClass() gives me "Class [Lmyclass;" where myclass is a class created by me. My Question are a. Why doesnt it show the entire name of the class in eg. 1 b. What determines the prefix appended to the class names as in eg. 2 &3 Thanks
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16684
|
|
My Question are a. Why doesnt it show the entire name of the class in eg. 1 b. What determines the prefix appended to the class names as in eg. 2 &3
When you call the getClass() method, you will get a Class object that gives you a ton of information about the class -- its fields, its methods, its interfaces, etc. To answer (a), one of the methods of the Class object that is returned, gives you the "entire name of the class". To answer (b), I am assuming that you simply did a system out of the class object and got a string (that is pretty useless). When you print the Class object, all you see is the result of the toString() method. The result of the toString() method simple returns "Class" followed by the class descriptor (as a string) for the class. Don't just println() the class object, use the methods of the class object to obtain the information about the class. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16684
|
|
.. also why does it have ";" in the end
BTW, for the full details on why it ends with ";", or start with "[L", etc., google for "Java class descriptor" -- for the string format of the descriptor. Henry
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
a) this is an array of *primitives* - there really isn't a true class name in this case.
|
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
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Originally posted by Henry Wong: To answer (b), I am assuming that you simply did a system out of the class object and got a string (that is pretty useless). When you print the Class object, all you see is the result of the toString() method. The result of the toString() method simple returns "Class" followed by the class descriptor (as a string) for the class.
Except he's not printing an instance of his own class, he's print an instance of an array of his own class. Sadly, arrays have a quite useless toString() implementation. Instead, use the static Arrays.toString methods. That will print arrays just like the default collection implementations are printed.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: using getclass on references
|
|
|