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

Bhargav Modi

I know that method variables are stored on stack of the memory but slightly confused over final. I had browsed many links like this could not get proper understanding? below is the example of inner class in which final variables are accessed and local non-final variables are not as they are stored in stack

class Employee {
public void getAddress(){
final int location = 13; 
int notFinalVar = 13;   
    class Address {
       System.out.println (location); 
       System.out.println (notFinalVar); // compiler error
    }
}

Update: Just now came to know about hidden fields called synthetic field( inner class heap memory area) in which copy of final variables are stored so does it finally means that final variables are stored in finally Stack memory Area?

Saif

Reading through some answers of SO and articles, My understanding is :

The answer is stack. All local variable (final or not) stored into the stack and go out of scope when the method execution is over.

But about final variable JVM take these as a constant as they will not change after initiated . And when a inner class try to access them compiler create a copy of that variable (not that variable it self) into the heap and create a synthetic field inside the inner class so even when the method execution is over it is accessible because the inner class has it own copy.

so does it finally means that final variables are stored in finally Stack memory Area?

final variable also stored in stack but the copy that variable which a inner class have stored in heap.


synthetic field are filed which actually doesn't exist in the source code but compiler create those fields in some inner classes to make those field accessible. In simple word hidden field.

References :

How does marking a variable as final allow inner classes to access them?

Cannot refer to a non-final variable inside an inner class defined in a different method

Mystery of Accessibility in Local Inner Classes

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Where are method-local variables stored?

From Dev

Where are the addresses of local and other variable types stored?

From Dev

Where are the addresses of local and other variable types stored?

From Dev

Why Local Inner class requires local variable Final? This Variable is in same method in which Local Inner class define

From Dev

Local variable could be final

From Dev

Final local variable error

From Dev

When should i use final variable instead of local variable inside a method

From Dev

Where are the (local) Meteor Collection stored?

From Dev

Where are files for local branches stored

From Dev

behaviour of local final variable in java

From Dev

behaviour of local final variable in java

From Dev

Where is final parameter stored in anonymous class instance?

From Dev

In Java can making a local variable final in a non-static method that is called many times cause a memory leak?

From Dev

Cannot refer to the non-final local variable button defined in an enclosing scope, Random method error

From Dev

Variable as Where clause in stored procedure

From Dev

Extension method and local 'this' variable

From Dev

undefined local variable or method

From Dev

Calling a method (stored in a variable) in a closure

From Dev

Get the name of a method stored in a variable

From Dev

performance or optimization in Android : final local variable vs local variable

From Dev

Where are cookies and local storage stored for Chrome?

From Dev

Where does the docker images stored in local machine

From Dev

semantic of local final variable in the Java Memory Model?

From Dev

How can I mock a local final variable

From Dev

Local variable log defined in an enclosing scope must be final or effectively final

From Dev

Threads: Local variable defined in an enclosing scope must be final or effectively final

From Dev

Local variable i defined in an enclosing scope must be final or effectively final

From Dev

Local variable log defined in an enclosing scope must be final or effectively final

From Dev

Local variable defined in an enclosing scope must be final or effectively final

Related Related

  1. 1

    Where are method-local variables stored?

  2. 2

    Where are the addresses of local and other variable types stored?

  3. 3

    Where are the addresses of local and other variable types stored?

  4. 4

    Why Local Inner class requires local variable Final? This Variable is in same method in which Local Inner class define

  5. 5

    Local variable could be final

  6. 6

    Final local variable error

  7. 7

    When should i use final variable instead of local variable inside a method

  8. 8

    Where are the (local) Meteor Collection stored?

  9. 9

    Where are files for local branches stored

  10. 10

    behaviour of local final variable in java

  11. 11

    behaviour of local final variable in java

  12. 12

    Where is final parameter stored in anonymous class instance?

  13. 13

    In Java can making a local variable final in a non-static method that is called many times cause a memory leak?

  14. 14

    Cannot refer to the non-final local variable button defined in an enclosing scope, Random method error

  15. 15

    Variable as Where clause in stored procedure

  16. 16

    Extension method and local 'this' variable

  17. 17

    undefined local variable or method

  18. 18

    Calling a method (stored in a variable) in a closure

  19. 19

    Get the name of a method stored in a variable

  20. 20

    performance or optimization in Android : final local variable vs local variable

  21. 21

    Where are cookies and local storage stored for Chrome?

  22. 22

    Where does the docker images stored in local machine

  23. 23

    semantic of local final variable in the Java Memory Model?

  24. 24

    How can I mock a local final variable

  25. 25

    Local variable log defined in an enclosing scope must be final or effectively final

  26. 26

    Threads: Local variable defined in an enclosing scope must be final or effectively final

  27. 27

    Local variable i defined in an enclosing scope must be final or effectively final

  28. 28

    Local variable log defined in an enclosing scope must be final or effectively final

  29. 29

    Local variable defined in an enclosing scope must be final or effectively final

HotTag

Archive