Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compile time & runtime type?

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the meaning of compile-time type & runtime type?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "compile-time type" of a variable is the type it is declared as. The "runtime type" is the type of the actual object the variable points to. So in the following line of code:

The compile-time type of "o" is Object, and its runtime type will be String.
The compile-time type governs which methods can be called, while the runtime type governs what happens during the actual call. You can write

because Object has a hashCode method, but you can't call

because although String has an indexOf() method, Object doesn't, and so the compiler won't let you call it.
Now, when the "hashCode()" function is actually called, it's String.hashCode() that gets executed. That's polymorphism, and if you haven't learned about that yet, you will soon!
 
Your mother was a hamster and your father was a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic