-1

i have a Problem with understanding the function of the stack Memory in Java. According to my teacher, the following method would create 2 primitive type local variables in the stack Memory.

  private void test()
    {
        int x = 0;      //created in stack
        int y = 1;      //created in stack
    }

As trough Definition stackmemory is "last in, first out" i dont understand how this should work. I can Access "x" which is definately not the variable, which was "last in" in the stack Memory.

private void test()
    {
        int x = 0;      //created in stack
        int y = 1;      //created in stack

        x = 15;         //x is not last in (y is last in)!
    }

Can somebody explain me, what am i misthinking?

RandomDude
  • 109
  • 2
  • 9
  • 3
    This "stack" does not apply to variables declared within the same method - they are all in the same *stack frame*. If you call another method from `test`: this is where another stack frame is pushed on the stack. – laune Dec 27 '14 at 12:26
  • 1
    http://stackoverflow.com/questions/19433523/where-is-thread-object-created-stack-or-heap - This thread would give a good understanding of execution model – user1428716 Dec 27 '14 at 12:31
  • 2
    You're confusing a stack on which you, the developer, push and pop values, with the call stack, which is managed by the JVM. If you look at the generated byte code you might have a better understanding. – Dave Newton Dec 27 '14 at 12:31
  • 1
    Stack frames are pushed onto the stack (= the stack pointer is advanced) whenever a method is called, with all variables (and parameters) being allocated at the same time. The frame is removed (= the stack pointer is reduced) when control returns from the method. - The stack is also "unwound" if an exception is thrown and propagated up to some upper invocation level. – laune Dec 27 '14 at 12:31
  • 2
    I think your are confusing between the [memory area called stack](http://en.wikipedia.org/wiki/Stack-based_memory_allocation) and the [stack data structure](http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29). The first makes use of the latter. – A4L Dec 27 '14 at 12:32

2 Answers2

1

Variable declaration order has nothing to do with stack memory. Executing method variables (both x and y) will reside within one stack frame and it will be at the top of the stack.

If this method calls another method, then a new stack frame will be created and that will be on the top of the stack. Once that method completes, corresponding stack frame will be removed. Until this method completes execution, we cannot access the caller method variables. This is where the LIFO ordering of stack is used.

Kumar V
  • 1,570
  • 1
  • 12
  • 19
0

The Stack in this instance represents a chunk of memory that is accessible to the currently executing method - more correctly referred to as a Stack Frame. Each called method gets its own Stack Frame in which to store local data.

All the local variables in the method belong to this stack frame, but the frame's behaviour is not restricted to Last-In, First-Out - any area within the frame can be read or written which is why your variables can be accessed in any order.

It's probably easier to think of it as a chunk of memory. When method A() calls method B(), A's frame is stored (on a real stack-like structure) and a new frame created for B. When B returns, A's frame is restored - that's where your typical stack LIFO behaviour comes in.

adelphus
  • 10,116
  • 5
  • 36
  • 46