Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are Java final local variables stored?

Take the following example:

public void init() {
    final Environment env = new Environment();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
             env.close();
        }
     });
}

Firstly, where is env stored? Is it:

  • copied by the compiler into a hidden member variable of the inner class that references it
  • copied to, and referenced on, the heap
  • left on the stack and somehow referenced there
  • something else

My guess is the first option.

Secondly, do any performance issues that arise from doing this (rather than simply creating env as a member variable of the class and referencing it as such) particularly if you are creating large numbers of such inner class constructs that reference final local variables.

like image 346
Joel Avatar asked Dec 22 '09 11:12

Joel


People also ask

Where are local variables allocated in Java?

Local variables are created in the stack. Instance variables are created in the heap & are part of the object they belong to. Reference variables are created in the stack.

Can local variables be final in Java?

Local VariablesWhen final is applied to a local variable, its value must be assigned exactly once. We can assign the value in the final variable declaration or in the class constructor. In case we try to change the final variable value later, the compiler will throw an error.

Where are local variables located?

In most languages, local variables are automatic variables stored on the call stack directly. This means that when a recursive function calls itself, local variables in each instance of the function are given distinct addresses.

Where are local and instance variables stored in Java?

Instance variables are declared in the class, but outside of the constructors, methods, or blocks of the particular class. They are used to represent the state of an object. Instance variables are stored in the heap section of the memory.


1 Answers

Yes, they are copied, which is why you have to declare the variable as final. This way, they are guaranteed to not change after the copy has been made.

This is different for instance fields, which are accessible even if not final. In this case, the inner class gets a reference to the outer instance that it uses for this purpose.

private Environment env;  // a field does not have to be final

public void init() {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
             env.close();
        }
     });
}

Secondly, do any performance issues that arise from doing this?

Compared to what? You need to have the field or variable around for your inner class to work, and a copy is a very efficient way. It is only a "shallow" copy anyway: just the reference to the (in your example) Environment is copied, not the Environment itself.

like image 117
Thilo Avatar answered Oct 15 '22 04:10

Thilo