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

if method is on Stack and object is on heap then where do Static method is stored  RSS feed

 
ganesh pol
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if method is on Stack and object is on heap then where do Static method is
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All Java bytecode is part of the Class object instantiated by the JVM when loading a class, and all object instances are allocated on the heap. Similarly, all native code produced by a JIT is placed on the heap.

Anything in a thread's stack is by definition temporary and will disappear when the method call that put it there returns. Local variables and the method call chain make up the majority of the data on a stack; everything else goes on the heap so its lifecycle can be controlled by objects other than Threads.
[ May 02, 2005: Message edited by: David Harkness ]
 
ganesh pol
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir i don't understand yet
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Explain exactly what you mean by "method is on Stack". Are you talking about the bytecode that performs the work for the method, built from the Java source code by the javac compiler?
 
ganesh pol
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir
since main is also Static method it is on Top of Stack(TOS)
no matter whether it is Static or non Static method method always Stored on TOS
is it Correct?

 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not answering questions seeking further understanding of your question doesn't improve the chances of us being able to answer it! If you answer my previous question "Explain exactly what you mean by 'method is on Stack'", you'll get better success.

That being said, I'll make an assumption given your followup: by "method is on Stack" you are refering to the information placed on the stack for a single method call: the values for the method's parameters and any local variables it defines. You are not talking about the executable bytecode for a method. If this assumption is false, I can't help without further information.

If the assumption is true, there is absolutely no difference in how static and instance (non-static) methods use the stack.* The stack contains the complete runtime state for a single thread (there is one per thread) from the first method executed -- main() or Thread.run() -- to the currently running method.

* There is possibly one minor difference: instance methods have access to a "this" local variable. It may or may not be placed on the stack (I have never needed to know), but I don't think this is related to what you were asking about.
[ May 03, 2005: Message edited by: David Harkness ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!