Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum number of local variables in a Java Method

Tags:

java

I understand the importance of Single Responsibility Principle, but technically speaking do we have any upper bound on the number of local variables (that which are stored in stack frames) within each java method. And is that upper bound equal to the maximum stack size, ie., can i have a stack frame of size equal to the maximum stack size configured?

like image 612
Bajji Avatar asked Oct 18 '12 18:10

Bajji


2 Answers

There is no upper bound in defining number of local variables. If you define too many variables which couldn't fit in a stack frame (or) JVM couldn't allocate a stack frame for that size, it will throw StackOverflowError and exit.

There is good lecture by a stanford professor which may help you.

like image 169
kosa Avatar answered Sep 26 '22 11:09

kosa


This will really be defined by your runtime and how much stack space is allocated, per process.

like image 43
samoz Avatar answered Sep 25 '22 11:09

samoz