• Post Reply Bookmark Topic Watch Topic
  • New Topic

hashCode() static reference from abstact class  RSS feed

 
Muni K Reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Muni K Reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Test.class is a static reference to a Class object. You are calling the hashCode() method of this object.
 
Muni K Reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Sheriff
    Posts: 16028
    87
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
  •  
    Muni K Reddy
    Ranch Hand
    Posts: 74
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 74
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!