• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

hashCode() static reference from abstact class

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Create symphonies in seed and soil. For this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic