• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do static methods contain their own stack?  RSS feed

 
Chris Avila
Greenhorn
Posts: 16
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as the question states, I have been searching and have been unable to find an answer to this question. Do static methods contain their stack? Thanks in advance
 
Henry Wong
author
Sheriff
Posts: 22814
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Each thread has their own stack. Calling a method, whether static or not, does not create a new stack.

Henry
 
Chris Avila
Greenhorn
Posts: 16
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, actually each method invocation creates it's own stack right? Because of a new stack being allocated for each method invocation, we can observe the existence of a StackOverflow error, where a method is invoked 1 too many times and the application runs out of stack memory.
 
Henry Wong
author
Sheriff
Posts: 22814
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each method invocation pushes stuff onto the stack. This includes the return address, the parameters, etc. Returning from the method pops stuff off of the stack. Method calls don't create a new stack...... note: to be exact, each method call creates a new stack frame -- and not a new stack.

Additionally, a stack overflow happens when the stack is out of memory when stuff is pushed. It doesn't mean that the JVM tried to create a new stack, which failed.

Henry
 
Chris Avila
Greenhorn
Posts: 16
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Cool stuff! Thanks for clearing this up for me Henry! I would have been living in mis-belief if you had not cleared that up . Sorry to ask so many questions, but would the first invocation of a method create a new thread? How exactly is a thread created in Java when the Thread class is not used?
 
Henry Wong
author
Sheriff
Posts: 22814
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Avila wrote: How exactly is a thread created in Java when the Thread class is not used?


Unless there is something that I missed, I do not believe that there is any way to run Java code, in a new thread, without directly or indirectly going through the Thread class.

Having said that, with all modern JVM implementations, the Thread class is just a wrapper around the thread library provided by the system. So, in theory, it is possible to bypass the Thread class, with something like a JNI call -- but then, technically, this isn't really pure Java code.

Henry
 
Chris Avila
Greenhorn
Posts: 16
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry, I appreciate your time and answers
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!