why JVM do this? Is OS responsible for it?
Why global variable stores in heap and local variables in stack?
There are just two places where data can reside in JVM: the heap and the stack (well, JNI tricks inclusive it can reside anywhere, but this is an exception). All local variables in methods are located on the stack, everything else - on the heap. In both your examples array data is allocated on the heap, though in the second example the reference maze
is a method local variable so is itself located on the stack.
you can refer to this for more info: