• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Stack/Heap -Methods in the class?

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

1.The methods defined in the class ,do they go on stack or heap?
I read somewhere ,Object with its instance-variables and methods is stored on heap .
2. Also i have another doubt

example code is :
<code>

class Dog{
public static void main(String args[])
{
Dog d=new Dog(); //Object reference to Dog
}



}

</code>

Does his mean there are 2 references to Dog -objects in memory.one is ' this' instance run by JVM ,another user-programmed d.
Thanks,
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Methods are executable code, which is stored in a separate memory area from data (variables). Note that there is not a copy of the code of the methods of a class for each instance of that class - that would be a waste of memory, because the code is the same for all instances of a class. Executable code is certainly not stored on the stack.

2. You have one variable 'd' which refers to one Dog object. 'this' refers to the current object. If you'd call a non-statice method on d, then inside that method you could use 'this', which would refer to the same Dog object as d. There are ofcourse not 2 Dog objects.
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic