File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes compile time & runtime type? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compile time & runtime type?" Watch "compile time & runtime type?" New topic

compile time & runtime type?

Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
what is the meaning of compile-time type & runtime type?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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!

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: compile time & runtime type?
It's not a secret anymore!