• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The most Basic Java Question

 
Greenhorn
Posts: 28
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this question is bugging me for past few days and i couldn't find a proper answer for this.

A class can be thought like a blue print of an Object, and it contains state (data member) and behaviour(methods). Every object instantiated from a specific class contains it's own set of Data Members i.e. data members will have their own physical address in the memory which is differnt from Object to Object. But can anyone tell me what about the Methods in a class? Do every object has their own set of methods (different physical address to store methods) or they all refer to the same address to get the method.

P.S: i m not talking about the static methods and data members.

Can anyone provide a proper clarification on this subject. i m a beginer and want to know how members and methods are stored in memory.
 
author
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code for the methods is shared by all of the members of a class. That is the general answer.

The more exact answer is that all of the code is shared by objects that have exactly the same class. In Java (as with other polymorphic languages) an object can have many classes that it is a part of, and these classes form the class inheritance hierarchy for the object. Two objects that are of the same class will share the code for their methods unless one is a member of a sub-class which has its own implementation of that method.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a good explanation, Jim!
 
prabal nandi
Greenhorn
Posts: 28
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Jim for the explaination.. Another question related to same.
When 2 objects extending same class (method codes are shared) are accessing same method; how will JVM decide the service? Because we haven't explicitly declared the method as synchronized.
 
Jim Waldo
author
Posts: 29
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prabal nandi wrote:Thanks a lot Jim for the explaination.. Another question related to same.
When 2 objects extending same class (method codes are shared) are accessing same method; how will JVM decide the service? Because we haven't explicitly declared the method as synchronized.



Whether or not this even could be a problem will depend on whether or not you are using multiple threads in your program, and running on a multi-core machine. If you aren't using multiple threads, or are on a single-core machine, then only one thing is running at any one time. The OS (and the underlying hardware) will make sure that you are not going to run into problems.

On a multi-core processor running multiple threads, there is still an easy answer for the code-- the code is simply copied from one core to the other. So you may have two instances of the code running at the same time. But the code is immutable, so there is no problem with synchronization. Since the code is the same everywhere, it doesn't matter how many copies you have.
 
prabal nandi
Greenhorn
Posts: 28
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Jim for the explaination...
 
reply
    Bookmark Topic Watch Topic
  • New Topic