| Author |
hashCode() static reference from abstact class
|
Muni K Reddy
Ranch Hand
Joined: Aug 23, 2007
Posts: 74
|
|
Result: 17332331 Sorry if im being silly guys! I was playing around with hashCode and wrote the above code. The hashCode() above belongs to which object? Test being an abstact class cannot have an object and if it belongs to its superclass Object then how can I make a static reference to a nonstatic method hashCode()? Many Thanks [ August 23, 2007: Message edited by: Muni K Reddy ] [ August 23, 2007: Message edited by: Muni K Reddy ]
|
 |
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
|
|
Test.class is the java.lang.Class object for the Test class. Being a Java object, the java.lang.Class object has a hash code. So, you are getting the hashcode for the Class object associated with the Test class. It's perfectly legal Java to do this. I can't think of any reason you'd need to do so, in a real program.
|
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
|
 |
Muni K Reddy
Ranch Hand
Joined: Aug 23, 2007
Posts: 74
|
|
Originally posted by Peter Chase: Test.class is the java.lang.Class object for the Test class. Being a Java object, the java.lang.Class object has a hash code. So, you are getting the hashcode for the Class object associated with the Test class. It's perfectly legal Java to do this. I can't think of any reason you'd need to do so, in a real program.
Thanks Peter, When I say Test.class.hashCode(), does it not mean a static reference?
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
|
Test.class is a static reference to a Class object. You are calling the hashCode() method of this object.
|
Joanne
|
 |
Muni K Reddy
Ranch Hand
Joined: Aug 23, 2007
Posts: 74
|
|
Originally posted by Joanne Neal: Test.class is a static reference to a Class object. You are calling the hashCode() method of this object.
Thanks mate! I did try to go through the link you sent about Class class. I can gather that Class is a subclass of Object and thats where the hasCode() comes from. I would like to get the concept cleared and would be glad if you(or anyone is Javaranch) could find time What is the relation between Class class and my abstract class Test? ]hashCode is a not static method from Object class (ie java.lang.Class object in our example), then why was I able to make a static reference to it?Can I make two classes Boo and Foo and make a static reference of Boo point to a method in Foo?(if I can then what would be their relation? Many many thanks! Muni
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12952
|
|
Originally posted by Muni K Reddy: What is the relation between Class class and my abstract class Test?hashCode is a not static method from Object class (ie java.lang.Class object in our example), then why was I able to make a static reference to it?Can I make two classes Boo and Foo and make a static reference of Boo point to a method in Foo?(if I can then what would be their relation?
For every class, there is a java.lang.Class object that represents the class. It is used when doing things via reflection.You did not make a static reference to the hashCode() method; you are simply calling hashCode() on an object of type Class.I don't know what you mean by this. Can you give a code example to explain what you mean?
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Muni K Reddy
Ranch Hand
Joined: Aug 23, 2007
Posts: 74
|
|
Originally posted by Jesper Young: For every class, there is a java.lang.Class object that represents the class. It is used when doing things via reflection.You did not make a static reference to the hashCode() method; you are simply calling hashCode() on an object of type Class.I don't know what you mean by this. Can you give a code example to explain what you mean?
I really do appreciate your answer mate. java.lang.Class object that represents the class.?? What does this mean?When I say Test.class is it not static reference? I don't know what you mean by this. Can you give a code example to explain what you mean? >> Actually I would like to make some code example if I can. Using your first point - For every class, there is a java.lang.Class object that represents the class., can I make a Foo class represent another class Boo just like java.lang.Class does for Test class here? sorry mate, just trying hard to get it in.. Many thanks for answering
|
 |
Muni K Reddy
Ranch Hand
Joined: Aug 23, 2007
Posts: 74
|
|
Let me try to rephrase my question hopefully making it more understandable. Whatever relationship there is between abstact class Test and java.lang.Class , can i create the same relationship between two custom class say Boo and Foo Just like Test.class.hashCode(), I would like to know if Boo.foo.aMethod()is possible where Boo is abstract class and foo is an object representing class Foo and aMethod() is one of its methods. Thanks [ August 23, 2007: Message edited by: Muni K Reddy ]
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Only if foo is a static reference and is initialised either at the point it is declared or in a static block. i.e. or
|
 |
 |
|
|
subject: hashCode() static reference from abstact class
|
|
|