i am trying to understand logic behind instance variables scope in this case :
public class DiceActivity extends AppCompatActivity implements View.OnClickListener {
private TextView textResult;
private int max;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dice);
int max = getIntent().getIntExtra("max", 0);
TextView textTitle = (TextView) findViewById(R.id.textTitle);
textTitle.setText(max+" sided dice");
textResult = ((TextView) findViewById(R.id.textResult));
textResult.setText("");
Button buttonRoll = (Button) findViewById(R.id.buttonRoll);
buttonRoll.setOnClickListener(this);
}
@Override
public void onClick(View v) {
SecureRandom random = new SecureRandom();
int result = random.nextInt(max) + 1;
textResult.setText(String.valueOf(result));
}
}
the instance variable "max" is not linked to "max" in onCreate method (max returns 0 unless i declare max2=max to get correct result) but somehow textResult works well and doesn t need to declare another variable to get the result.