wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compile time & runtime type?" Watch "compile time & runtime type?" New topic
Author

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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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: http://aspose.com/file-tools
 
subject: compile time & runtime type?
 
Similar Threads
Final Class cannot be instantiated
Generics problem (? extends Object)
Dynamic Method Lookup?
Conditional Operator
output of program?