Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing local variables from method local inner class in which we declare the inner class

I am learning Java, the tutorial i am following says Java does not allow the variable k (local variable to m1() in the below example) to be directly accessible from method m2() because they are created in the same method m1() and that I will get an error during compile time (unless k is declared as final). The reason they say is a local variable (k) is created during method is called and destroyed after the method execution is complete, but an object (o) is created when the object is instantiated and can still be not destroyed after method execution. So the tutorial says that if you call the method m2() or object o after method m2 is executed (I do not know how it is possible) variable k will be destroyed and will not be available. So the tutorial claims that Java will not allow such declarations. (Please feel free to correct me wherever my understanding is wrong)

But when I compile this program works fine. Am I missing something? I understand this is a bit complex from my perspective of explaining so if my question is clear so please feel free to ask me if something is not clear.

Thanks in advance for your help.

class innerclass_ex8
{
    int x = 10;
    static int y = 20;

    public void m1()
    {
            int k = 30;
            final int z = 50;
            class inner {
                public void m2() 
                {
                    System.out.println(x);
                    System.out.println(y);
                    System.out.println(k);
                    System.out.println(z);
                }
            }
            inner o = new inner();
            o.m2();

    }
    public static void main(String[] args)
    {
        innerclass_ex8 g = new innerclass_ex8();
        g.m1();
    }
}
like image 473
Srinivas Avatar asked Jul 10 '16 12:07

Srinivas


People also ask

Can an inner class declared inside of a method access local variables of this method?

Yes, we can access the local final variables using the method local inner class because the final variables are stored on the heap and live as long as the method local inner class object may live.

How do you access a local inner class method?

We can access method local inner class only within the method where we declare. Outside of the method, we cannot access it because it is local to that method. Therefore, a method local inner classes are the most rarely used in all types of the inner class because of its less scope.

Can you have an inner class inside a method and what variables can you access?

It can access any private instance variable of the outer class. Like any other instance variable, we can have access modifier private, protected, public, and default modifier. Like class, an interface can also be nested and can have access specifiers.

Can local variables be declared inside a method?

Local Variables We can put it in a method body. We declared num in the method body of main(). Variables declared inside a method are called local variables. Local variables can only be used within the method body.


1 Answers

First of all, your program compiles and works fine because you are using Java 8. If using Java 7 or lower, it won't even compile.

The reason is exactly as you cited. But I will try to explain it a bit more. Consider the following code:

public void m1() {
    int k = 30;
    class inner {
        public void m2() {
            System.out.println(k);
        }
    }
    inner o = new inner();
    k = 42;     // <= Note the reassignment here.
    o.m2();
}

What should the method call o.m2() print? "30" or "42"? Both outputs could reasonably be argumented. At the time the method was declared and defined, the variable k had the value 30. At the time the method was called, the variable k had the value 42.

To prevent such ambiguities, the compiler does not allow assignment to a variable that is used in such inner classes (local and anonymous). So it must be final.

In Java 8 this was relaxed a bit. Java 8 introduced the concept of effectively final. A variable that is declared and initialized and not being assigned again is considered effectively final. And the compiler allows that code without declaring the variable final.

As a matter of fact, you also get a compiler error in Java 8 when trying to compile the above code.

like image 178
Seelenvirtuose Avatar answered Oct 28 '22 23:10

Seelenvirtuose