Question

Hey :) I've got a Problem with Variables. I wanna know how the methode run() can see 'l' and 'a'.

public static void startDraw(String[] players) {
    final DrawGame a = new DrawGame(players); 

        for ( Player b : a.getPlayers()){
                 final Player l =  b;
                 Runnable q = new go(); 


                    }
    }

    protected static class go implements Runnable{

        public go(){

        }
        public void run(){
            while (a.finished()!= true){
                l.drawLine();
            }
        }

        Thread t = new Thread(new go()).start();
    }
Was it helpful?

Solution

I wanna know how the methode run() can see 'l' and 'a'?

Because your inner class go is method local inner class and a and l are method local final variable in the same method go()

If you declare the variables final, the inner class knows they won't change, so they can just make a copy of it for private use, which will exist after the original value has been removed from the stack.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top