Java Where local variables, object references, instance variables

I am currently studying the concepts of java memory, stack and heap, I know that local variables and method calls lived in a place called a stack. and the objects lived inside the heap. but what if this local variable contains an object? or has a link to an object?

public void Something(){
        Duck d = new Duck(24);
}

Does he still live inside the stack? and where do instance variables live? Please keep it as simple as possible. thank.

+5
source share
2 answers

A local variable d(allocated on the stack) contains a reference to the class object Duck. In general, objects are allocated on the heap.

Java 6e14 -, "escape-". -XX:+DoEscapeAnalysis switch, , JVM , , , , "" , , . JVM ( , ). , , .

, , .

+11

. , - ​​ , stack.else, refrence , .

+2

All Articles