-
getClass and getName
I don't understand how the following works:
class RTTI {
X x = new X();
Class clObj;
clObj=x.getClass();
System.out.printlin("x is object of type:" + clObj.getName());
{
output : x is object of type: X
I don't understand how the output would be of class X when the class of clObj is Class.
I am new at this, so please take it easy on me.
thanks,
jonnyhall
-
Don't think so hard. What would be normal behaviour?
Code:
Object o = new String("Hello");
System.out.println(o.toString());
What would you expect to be printed out? Object's toString() method i.e. Object@32423? No! You'd expect String's toString method i.e. Hello.
The same is true of your code. clObj is a Class reference variable, which points to the value returned form x.getClass(). It is this on which the method .getName() is invoked. It's usual polymorphic behaviour.
Hope this clears things up,
ArchAngel.
ArchAngel.
O:-)
-
So clObj just points to the X class of x when you do clObj=x.getClass? And clObj.getName returns the name of the class that clObj is pointing to? What is throwing me off was my book says that the getName method is used to get the name of the class of the invoking object, so I could not understand why clObj.getName didn't return Class as the name of clObj's class.
-
Your book is correct....and so am I. It is returning the name of the class of the object being invoked. The object being invoked is the object which the variable 'clObj' references.
Make sure you understand the difference between a reference variable and the objects to which they point.
Try this:
Code:
clObj=x.getClass();
if (clObj instanceof X) {
System.out.println("It's of type X!");
}
ArchAngel.
O:-)
-
holy crap, use better vairable names when writing sample code. that's really hard to follow
but anyways.
name = something.getClass();
this just stores a value into name.
something.getClass() is returning the name of the class that "something" is in, and then storing that name into "name"
name.getClass() has nothing to do with the value of "name" it does the same thing as
something.getClass(), just to "name"
I'm surprised more of you people don't get hit by cars.
-
read the API on the object class. there is no getName method. only a getClass
I'm surprised more of you people don't get hit by cars.
-
First of all to Archangel, I tried to use the "instanceof" code that you sent to me to test whether clObj was an instance of X. I could not even compile that code. It worked when I tried:
if(x instanceof X){
System.out.println("It is an instance of X!");
}
it also worked when I tried
(clObj instanceof Class)
Not only did they compile, but they ran as expected.
Fantik,
I understand that the getName method is a method of the Class class. Also, I am starting to see that a Class object is used to represent other classes that are loaded into a program during runtime. What I don't understand now is how the getName method works with a Class object after you use the getClass method on it. Are Class objects set up to deliver the name of the class they represent instead of the class that they are in which is obviously Class? I know that Class objects just point to other classes and hold info about them, but I also know that they do not become an instance of the class that they represent after you use the getClass method on them. If that were true, I would have been able to use the instanceof operator to test whether or not a Class object became an instance of the class it represents. I don't fully understand how the getName method works on a Class object after you use the getClass method.
-
OK. Here's a little explanatory program that should clear this all up:
Code:
public abstract class Explanation {
public static void main(String[] args) {
Object o = new Object();
String s = new String();
/**
* OK. Let's start with basic, common objects and the 'instanceof' operator...
*/
// Will return 'true' because 'o' references an object which is, or is a subclass of 'Object'
if (o instanceof Object) {
System.out.println("'o' references an object which is a type of Object");
}
// Will return 'true' because 's' references an object which is, or is a subclass of 'Object'
if (s instanceof Object) {
System.out.println("'s' references an object which is a type of Object");
}
// Will return 'true' because 's' references an object which is, or is a subclass of 'String'
if (s instanceof String) {
System.out.println("'s' references an object which is a type of String");
}
// Will return 'false' because 'o' does NOT references an object which is, or is a subclass of 'String'
if (o instanceof String) {
System.out.println("Won't ever print out!");
}
else {
System.out.println("'o' references an object which is NOT a type of String");
}
System.out.println();
/**
* Now let's deal with all that class stuff...
*/
// Will print out java.lang.Object, because that is the kind of object referenced by variable 'o'
System.out.println("o.getClass().getName() returns '" + o.getClass().getName() + "'");
// Will print out java.lang.String, because that is the kind of object referenced by variable 's'
System.out.println("s.getClass().getName() returns '" + s.getClass().getName() + "'");
System.out.println();
/**
* OK. Let's try something clever.
* Because of inheritance, we know that String is a subclass of Object.
* We may therefore use an Object reference to point at a String object.
*/
o = s;
System.out.println("'o' now points to the object pointed to by 's'");
System.out.println();
// Will return 'true' because 'o' references an object which is, or is a subclass of 'Object'
if (o instanceof Object) {
System.out.println("'o' references an object which is a type of Object");
}
// Will return 'true' because 'o' does references an object which is, or is a subclass of 'String'
if (o instanceof String) {
System.out.println("'o' references an object which is a type of String");
}
}
}
Hope this helps,
David.
ArchAngel.
O:-)
-
Archangel,
thanks for showing me more about the instanceof operator. I did find a way to test to see whether or not clObj from the above programs was an actual instance of the X class. I got a negative response. The program I wrote said that after I ran Class clObj=x.getClass(), clObj was not an instance of class X. I think that clObj just becomes a "descriptor (?)" object that represents class X. I tried to run the getName method on other types of objects but I got errors. I even tried to run it on an Object object and it didn't work. I think that the books I have are not wrong, but they could have elaborated more on the subject.
again, thanks so much for your help Archangel.
Jonnyhall
-
Stick with the 'instanceof' operator
Personally, I always use instanceof to check object type, rather than use .getClass() - .getClass() is not as flexible:
Code:
public class Explanation2 {
public static void main(String[] args) {
String s = new String("Hello");
Object o = new Object();
// Is 's' of type 'String'? (Note - I don't say ", or a subclass of")
if (s.getClass() == String.class) {
System.out.println("'s' is of the String class. It is of type: " + s.getClass().getName());
}
// Is 's' of type 'Object'? (Note - I don't say ", or a subclass of")
if (s.getClass() == Object.class) {
System.out.println("Will never print out");
}
else {
System.out.println("'s' is NOT of the Object class. It is of type: " + s.getClass().getName());
}
System.out.println();
// Is 'o' of type 'Object'? (Note - I don't say ", or a subclass of")
if (o.getClass() == Object.class) {
System.out.println("'o' is of the Object class. It is of type: " + o.getClass().getName());
}
// Is 's' of type 'String'? (Note - I don't say ", or a subclass of")
if (o.getClass() == String.class) {
System.out.println("Will never print out");
}
else {
System.out.println("'o' is NOT of the String class. It is of type: " + o.getClass().getName());
}
System.out.println();
System.out.println("Assigning 'o' to the object referenced by 's'");
System.out.println();
o = s;
// Is 'o' of type 'String'? (Note - I don't say ", or a subclass of")
if (o.getClass() == String.class) {
System.out.println("'o' is of the String class. It is of type: " + o.getClass().getName());
}
// Is 's' of type 'Object'? (Note - I don't say ", or a subclass of")
if (o.getClass() == Object.class) {
System.out.println("Will never print out");
}
else {
System.out.println("'o' is NOT of the Object class. It is of type: " + o.getClass().getName());
}
}
}
This will produce:
Code:
C:\david\docs\forums\instance_test>javac Explanation2.java
C:\david\docs\forums\instance_test>java Explanation2
's' is of the String class. It is of type: java.lang.String
's' is NOT of the Object class. It is of type: java.lang.String
'o' is of the Object class. It is of type: java.lang.Object
'o' is NOT of the String class. It is of type: java.lang.Object
Assigning 'o' to the object referenced by 's'
'o' is of the String class. It is of type: java.lang.String
'o' is NOT of the Object class. It is of type: java.lang.String
ArchAngel.
O:-)
-
So, in the last part of your sample program here:
o = s;
// Is 'o' of type 'String'? (Note - I don't say ", or a subclass of")
if (o.getClass() == String.class) {
System.out.println("'o' is of the String class. It is of type: " + o.getClass().getName());
}
// Is 's' of type 'Object'? (Note - I don't say ", or a subclass of")
if (o.getClass() == Object.class) {
System.out.println("Will never print out");
}
else {
System.out.println("'o' is NOT of the Object class. It is of type: " + o.getClass().getName());
Your are making "o" into an "s" object?
-
Not quite. I am assigning an Object reference variable to the object pointed to by a String reference variable:
Before:
o -------------------------> 'Object' object
s -------------------------> 'String' object
After:
o ---------------\
---------> 'String' object
s ---------------/
It's an important distinction - reference variable and object (note - lower-case 'o'). In Java you cannot, no matter how hard you try use objects directly. You can only use reference variables which point to the objects.
When I write 'o = s', I am assigning the address reference of the object pointed to by 's' to the reference variable 'o'. This means that 'o' and 's' now hold the memory address of the same object.
ArchAngel.
O:-)
-
Sorry, the diagrams didn't keep their formatting:
Code:
Before:
o -------------------------> 'Object' object
s -------------------------> 'String' object
After:
o ---------------\
>---------> 'String' object
s ---------------/
ArchAngel.
O:-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks