• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

basic OO

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wanted to be clear on this:
Does every object has a copy of its methods in the stack like its instance variable?
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean if all the methods and their bit-wise JVM instructions are copied for each instance of an object? No that really would be a waste of memory. But that's something else though then a method being static or not.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does every object has a copy of its methods in the stack like its instance variable?

No but for each object, what you will find in the stack each time a method is invoked, are the arguments passed to the method and its local variables.
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats you are saying there is only one copy of methods in the stack for the class.
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is more computer architecture then java, and not really necersarry to know for the exam. But I think the instruction code of the methods is not on the stack. On the stack are local variables and method return addresses. Methods are kept in another part of the memory. Object do have a virtual method table that has pointers via-via to the spots where these methods actually reside. But don't shoot me if I am wrong here!

But the only thing you need to know is: local variables are located on the stack, objects created with the new statement on the heap.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the methods are NOT placed given to each object to have its own copy as that of instance variables. As some other rancher had already pointed out, it would be a mere waste of memory.

They are shared among all instances. What you actually do is just invoke a method which takes care of transferring the control to the method definition present by supplying the necessary arguments.

Hope this clears.
 
mooooooo ..... tiny ad ....
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic