Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why are methods bound at run time but variables bound at compile time?  RSS feed

 
Frankie Chee
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read from the textbook:
'Method are bound at run time where as variables are bound at compile time. It is the type of object which will tell which method to be called but it is the type of reference which will tell you which variable is to be called'
Firstly I presume it is the JVM which will 'bound the method' at runtime and also 'bound the variables' at compile time.
But really, what are the reasons for such different treatments by the JVM? Is it because methods are meant to do more calculations than variables?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you really read those sentences verbatim from a textbook? They're not much better than nonsense. Perhaps they've lost something in translation.
In any case, it is true that precisely which method will be called often isn't decided until runtime. I'm sure you'll benefit a lot from reading the JavaRanch campfire story about polymorphism, which explains why this is and how it works.
It's also true that there's no similar mechanism for variables; the Java compiler knows precisely which variable in which class will be accessed at runtime when it's compiling code using the variable. So I imagine this is the point the textbook was trying to make. Once you've read the campfire story and understand how polymorphic methods work, I think you'll agree that doing the same thing for variables just doesn't make any sense.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!