Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
Nov 5, 1999

Method Storage

Question:
Does each instance of a class have its own copies of instance methods in the same way it has its own copies of instance variables?

Answer:
Every time you instantiate a class, new memory is allocated to store the object. Part of this storage includes space for copies of its member variables. Rather than create a new copy of each method into a class instance, which would be rather expensive, a virtual lookup table mapping methods to code blocks is allocated.

You can think of class methods as functions that take an invisible argument that references the invoking instance, i.e., the this reference. That makes it possible to store methods independent of instantiated classes. When a class instance invokes a method, a lookup is performed in its virtual function table to find the proper code block to execute.

Therefore, all class intances share the same code, but have their own data. Novel runtime optimization techniques have been applied to improve the performance of virtual function lookups in Java so that you only pay a lookup penalty the first time you invoke a method.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date