Jinnrry
Jinnrry

Reputation: 445

How to get the value of the x variable?

public static void test(int x, int y) {

    Thread thread = new Thread() {
        @Override
        public void run() {
            System.out.println(x);
        }
    };
    thread.start();

}

This is my code. I can't get value of x. How do I get the value of the parameter x in the method of the anonymous class of the function?

Upvotes: 0

Views: 155

Answers (1)

shb
shb

Reputation: 6277

Declare parameter x final

   public static void test(final int x, int y) {

        Thread thread = new Thread() {
            @Override
            public void run() {
                System.out.println(x);
            }
        };
        thread.start();

    }

Upvotes: 7

Related Questions