Java reference variable declared outside method lives on stack or on heap

Yashaswi Bhardwaj
 class  Test
 {
     int y=10; // defined as part of the class
     Test obj1 =new Test();

 public void function1(){
    int x = 5; // defined locally
   Test obj2=new Test();
 }

  public static void main(String[] args) 
{
    Test obj = new Test();
}
 }

we already know the varibale "y" will gon heap and variable x will go on stack But my question is where will the reference variable "obj" which is a reference to test class be sved? on heap or stack? Also where will the reference variables "obj1" and "obj2" be saved on stack or heap? Ofcourse the object goes to heap but what about reference variables when declared inside function it goes to stack but what about when declared outside function?Confused.Please Help.

Jim Garrison

The value of obj1 (the reference) is itself part of an instance of Test which was allocated on the heap. The value pointed to by obj1 is a different instance of Test, which is also on the heap.

The only things on the stack are parameters to methods and variables declared inside methods.

Note that your first invocation of new Test() will fail due to the recursive infinite loop you've set up. Each contruction of a new Test require the construction of an additional Test, ad infinitum. You'll probably get a StackOverflowError, as method invocations stake up stack space even with no parameters and local variables.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java variable placed on stack or heap

From Dev

Unable to reference one particular variable declared outside a function

From Dev

Where are the variable/reference names or types stored in memory for stack/heap variables?

From Dev

What memory used for variable declared outside of a method or function

From Dev

Where is the local final variable in method stored (Stack/Heap)?

From Dev

struct with reference members : heap or stack?

From Dev

Reference types on the heap or on in Stack confused?

From Dev

Variable method reference in java 8

From Dev

If a javascript variable lives outside the class, but within the closure of the module, is it private?

From Dev

How is it possible to reference to a variable outside of a code block when it is declared inside a block?

From Dev

Strings - Stack and heap in java

From Dev

Strings - Stack and heap in java

From Dev

Java Heap and Stack

From Dev

How to location of variable on stack or on heap

From Dev

variable declared by auto sometimes is by reference?

From Dev

How to access a variable which is declared in a method belonging to another Java class?

From Dev

Java8: change the reference of the variable outside of forEach

From Dev

Stack-Reference of a Heap-Object

From Dev

Java 8: Is it possible to assign a method reference to a variable?

From Dev

Java 8 Lambda Sort With Variable Method Reference

From Dev

Accesing a reference variable from inside a method java

From Dev

Use a variable declared in a function outside that function

From Dev

Cannot access variable declared outside of Switch Statement

From Dev

Are variables local to a class that are not declared as pointers, created on the heap or stack?

From Dev

Java stack and heap memory management

From Dev

Variable not working outside method

From Dev

Variable content outside of a method

From Dev

why is private final java.util.List< E > stack declared here as a variable?

From Dev

Pointer variable inside a function points to stack or heap?

Related Related

  1. 1

    Java variable placed on stack or heap

  2. 2

    Unable to reference one particular variable declared outside a function

  3. 3

    Where are the variable/reference names or types stored in memory for stack/heap variables?

  4. 4

    What memory used for variable declared outside of a method or function

  5. 5

    Where is the local final variable in method stored (Stack/Heap)?

  6. 6

    struct with reference members : heap or stack?

  7. 7

    Reference types on the heap or on in Stack confused?

  8. 8

    Variable method reference in java 8

  9. 9

    If a javascript variable lives outside the class, but within the closure of the module, is it private?

  10. 10

    How is it possible to reference to a variable outside of a code block when it is declared inside a block?

  11. 11

    Strings - Stack and heap in java

  12. 12

    Strings - Stack and heap in java

  13. 13

    Java Heap and Stack

  14. 14

    How to location of variable on stack or on heap

  15. 15

    variable declared by auto sometimes is by reference?

  16. 16

    How to access a variable which is declared in a method belonging to another Java class?

  17. 17

    Java8: change the reference of the variable outside of forEach

  18. 18

    Stack-Reference of a Heap-Object

  19. 19

    Java 8: Is it possible to assign a method reference to a variable?

  20. 20

    Java 8 Lambda Sort With Variable Method Reference

  21. 21

    Accesing a reference variable from inside a method java

  22. 22

    Use a variable declared in a function outside that function

  23. 23

    Cannot access variable declared outside of Switch Statement

  24. 24

    Are variables local to a class that are not declared as pointers, created on the heap or stack?

  25. 25

    Java stack and heap memory management

  26. 26

    Variable not working outside method

  27. 27

    Variable content outside of a method

  28. 28

    why is private final java.util.List< E > stack declared here as a variable?

  29. 29

    Pointer variable inside a function points to stack or heap?

HotTag

Archive