How to increase the final integer variable?

Eclipse offers final, but I cannot increase the variable i.

@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);
}

enter image description here

+3
source share
6 answers

A final is an object that cannot be changed after its initialization.

Final (Java)

What you can do is create a variable within the do / while loop, which is final, with a value iand send it to the function.

+5
source

The simplest 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
    }
}
+4
source

, i. :

@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);
}

:

  • - Java , i, localCopy.

, Thread...

: , . . .

+1

, . , . , i, ++, incremented i, .

+1

. , Counter , ?

0

:

private int i;

:

@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 after that, you can use your variable, as usual, without having to mark it as final.

0
source

All Articles