开发者

How can I increase the final integer variable?

Eclipse is offering final but I can't increase the i variable.

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    int i = 1;
    do {
        try {
            new Thread(new Runnable() {
                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            tv.setText(Integer.toString(i));
                        }
                    });
                }
            });
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}
开发者_运维百科

How can I increase the final integer variable?


A final is an entity that can not be changed after it is initialized.

Final (Java)

What you could do is create a variable within the scope of the do/while loop that is final with the value of i and send that into the function.


The easiest solution here is to create a class:

public class FinalCounter {

    private int val;

    public FinalCounter(int intialVal) {
        val=intialVal;
    }
    public void increment(){
        val++;
    }
    public void decrement(){
        val--;
    }
    public int getVal(){
        return val;
    }

    public static void main(String[] arg){
        final FinalCounter test = new FinalCounter(0);
        test.increment(); // 1
        test.increment(); // 2
        test.increment(); // 3
        test.increment(); // 4
        test.increment(); // 5
        test.decrement(); // 4
        System.out.println(test.getVal());  // prints 4
    }
}


I think it is possible to create a local copy of the variable i. Try this:

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    int i = 1;
    do {
        final int localCopy = i; // Create here a final copy of i
        try {
            new Thread(new Runnable() {

                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            // use here the copy
                            tv.setText(Integer.toString(localCopy));
                        }
                    });
                }
            }).start(); // Don't forget to start the Thread!
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}

By creating a final local copy:

  • the compiler won't complain anymore
  • because of Java copies by value, you will only increase i and not localCopy.

I suppose you want to start the Thread as well...

EDIT: Indeed, you were right. You have to create the local final copy inside the loop. Check the new code.


A final variable can only be initialized once not necessarily when you are defining it. It can be set any time within the constructor , but only once. In your case when you are incrementing i using i++, you are trying to assign the incremented value to i again which is not allowed.


You could create a counter class like that and increment it. This way, the reference of the Counter object could be final but you could still set its value ?


What I did was add a:

private int i;

Before this:

@Override
public void onClick(View v) {
    final TextView tv = (TextView) findViewById(R.id.tvSayac);

    i = 1;
    do {
        try {
            new Thread(new Runnable() {
                public void run() {
                    tv.post(new Runnable() {
                        public void run() {
                            tv.setText(Integer.toString(i));
                        }
                    });
                }
            });
            i++;
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } while (i < 16);
}

And you'll be able to use your variable as usual after that, without having to mark it as final.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜