0
public class TestThread
{
    public static void main (String [] args) 
    {
        Foo f = new Foo();
        Thread t = new Thread(new Runnable() 
        {
            public void run() 
            {
                f.doStuff();
            }
        });
        Thread g = new Thread() 
        {
            public void run() 
            {
                f.doStuff();
            }
        };
        t.start();
        g.start();
    }
}
class Foo 
{
    public void doStuff() 
    {}
}

We Know that local variable of a function is not in scope while other method is running and we also know that method local inner class can only use final variable of the method. My question is why this code compiles and runs even though f is local variable and it is not even final. It should be out of scope for the anonymous inner classes...

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • whats your question here > ? f is an object of type Foo..., it would have made more much sense if for example you declared a String x in the outer class and asked why its accessible within class Foo... – QuakeCore Sep 13 '15 at 19:22
  • @abhishekbafna :o such a weird comment from a user with your reputation..., thats java 101! – QuakeCore Sep 13 '15 at 19:23
  • @abhishekbafna seriously now ?, [this is whats wrong](http://stackoverflow.com/questions/10442758/why-must-a-java-file-have-the-same-name-as-its-public-class) – QuakeCore Sep 13 '15 at 19:28
  • and yes the code wont compile, f has to be made final ion order for it to compile btw – QuakeCore Sep 13 '15 at 19:28
  • @QuakeCore This is happened because of a typo in creating java file. Sorry for that. Thanks for bringing it up. – YoungHobbit Sep 13 '15 at 19:32
  • Code is not compiling. `Error:(10, 9) java: local variable f is accessed from within inner class; needs to be declared final`. – YoungHobbit Sep 13 '15 at 19:32

2 Answers2

0
public class TestThread
{
     boolean innerClassCanSeeMe =true;
    public static void main (String [] args) 
    {
        boolean  accessible=false;
        final Foo f = new Foo();
        Thread t = new Thread(new Runnable() 
        {
            public void run() 
            {
                f.doStuff();
            }
        });
        Thread g = new Thread() 
        {
            public void run() 
            {
                f.doStuff();
            }
        };
        t.start();
        g.start();
    }
}
class Foo 
{
    public void doStuff() 
    {
        System.out.println(  TestThread.innerClassCanSeeMe);//yay error, unless you make innerClassCanSeeMe static.
       System.out.println( new TestThread().innerClassCanSeeMe);
    }
}

I strongly suggest you work on your OOP, you can start from here, and this isnt a direct answer to your question, but if you spend a couple of hours on that tutorial i think you will be able to answer yourself, and ill even give you a vote up if you do answer yourself within 24 hours :)

QuakeCore
  • 1,886
  • 2
  • 15
  • 33
0

f is effectively final; according to Oracle documentation on Local Classes:

However, starting in Java SE 8, a local class can access local variables and parameters of the enclosing block that are final or effectively final. A variable or parameter whose value is never changed after it is initialized is effectively final.

Rafal
  • 63
  • 1
  • 8