Method Local inner Classes! Confusion.

Last month when I was preparing for SCJP exam, I came through many new things in Java Language, some things were pretty confusing too. While going through the Inner Classes chapter I came through a problem. As we know we can define a class within a method, likewise :

Class MyClass{

private int x;

public showValue(){

class MyInnerClass{

System.out.println(“Hi I am method local Inner Class”);

{

}

A method-local inner class can be instantiated only within the method where the inner class is defined i:e: no other code running in any other method—inside or outside the outer class—can ever instantiate the method-local inner class. But, the inner class object cannot use the local variables of the method the inner class is in. The local variables of the method live on the stack, and exist only for the lifetime of the method. As we know that the scope of a local variable is limited to the method the variable is declared in. When the method ends, the stack frame is blown away and the variable is history. But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable. Because the local variables aren’t guaranteed to be alive as long as the method-local inner class object, the inner class object can’t use them.

So we mark the local variables as final. When marked final now our class can use it.

But How?

I didn’t knew where the local final variables are stored. Actually if you go through the specification of the JVM you will come to know that JVM defines various runtime data areas that are used during execution of a program. Some of these data areas are created on Java virtual machine start-up and are destroyed only when the Java virtual machine exits. Other data areas are per thread. Per-thread data areas are created when a thread is created and destroyed when the thread exits.

Different areas are :

1. The PC Register

2. Stack

3. Heap

4. Method Area

5. Runtime Constant Pool

6. Native Method Stack

I will not explain each of them here, for this you can refer to the Java virtual Machine Specifications.

The point of our discussion is where are final variables stored. They are stored in Runtime Constant Pool .

A runtime constant pool is a per-class or per-interface runtime representation of the constant_pool table in a class file. It contains several kinds of constants, ranging from numeric literals known at compile time to method and field references that must be resolved at run time. The runtime constant pool serves a function similar to that of a symbol table for a conventional programming language, although it contains a wider range of data than a typical symbol table.

Each runtime constant pool is allocated from the Java virtual machine’s method area. The runtime constant pool for a class or interface is constructed when the class or interface is created by the Java virtual machine. The following exceptional condition is associated with the construction of the runtime constant pool for a class or interface:

  • When creating a class or interface, if the construction of the runtime constant pool requires more memory than can be made available in the method area of the Java virtual machine, the Java virtual machine throws an OutOfMemoryError.
Advertisements