Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compile time & runtime type?

 
Susilo Saja
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the meaning of compile-time type & runtime type?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic