Arjun Krishnan
Arjun Krishnan

Reputation: 359

Can i change value of final int

I am new to android and I am trying to create a game. It is a very simple Guess the number game.I want to change the value of the correct answer if he user guesses correctly. I am unsure how to do this. his is my code for on create:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button subm = (Button) findViewById(R.id.button1);
    final TextView tv1 =(TextView) findViewById(R.id.textView1);
    final EditText userG=(EditText)findViewById(R.id.editText1);
    Random rand=new Random();
    final int correctAnswer=rand.nextInt(100)+1;
    subm.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int userNum=Integer.parseInt(userG.getText().toString());
            if(userNum>100 || userNum<0){
                tv1.setText("Enter a number between 1 and 100");
            }
            else{
            if(userNum==correctAnswer){

                tv1.setText("YEAH! You got it right");
            }
            else if(userNum>correctAnswer){

                tv1.setText("Sorry,Your guess is too high");
            }
            else if(userNum<correctAnswer){
                tv1.setText("Sorry,Your guess is too low");
            }}
        }
    });
}

How do I change correctAnswer? I am forced to call it final and can't change the value.

Upvotes: 2

Views: 4372

Answers (5)

user4879038
user4879038

Reputation: 1

What you need is a container:

public class IntContaner {
    public int value;

    public IntContainer(int initialValue) {
        value = initialValue;
    }
}

And in your code, you write:

final IntContainer correctAnswer=new IntContainer(rand.nextInt(100)+1);

...which allows you to change the contents of "correctAnswer" in the OnClickListener.

Upvotes: 0

kalyan pvs
kalyan pvs

Reputation: 14590

Can i change value of final int??

For this answer is NO..

I can understand your problem you are using it in anonymous inner class so eclipse forcefully ask you to make it final..So you want to use correctAnswer value within anonymous inner class so remove the final and define your correctAnswer globally like ..

private int correctAnswer;

then you can change value and you can access it in anonymous inner class

Upvotes: 10

Vivek Warde
Vivek Warde

Reputation: 1504

You cannot change the value of final int because Java uses the keyword 'final' for declaring constants & if you try to modify it then the compiler will generate error !

Or its better to not make it final !

Upvotes: 0

BabyDuncan
BabyDuncan

Reputation: 141

final for var allocated in stack,you can never change it ,i mean the four kinds eight basic type var . final for references(or pointers) ,you can never change the reference,but what the reference point to means , you can changed it . for example , final List list = new ArrayList(); list is final ,it must be one ArrayList,but what in the list , you can change it .

Upvotes: 1

Sujith PS
Sujith PS

Reputation: 4864

If we declare a variable as final , we can't change its value.

Remove the final keyword , then you can change its value.

Upvotes: -1

Related Questions