I know this has probably been asked before but I can’t find a specific answer to my specific question. I have already tried to grasp Java’s handling of references but this still puzzles me. Consider the following:
public class Question
{
private boolean isCorrect;
public void setCorrect (boolean _isCorrect) {
isCorrect = _isCorrect;
}
}
Now, in another file, somewhere in the code:
/**
* questionList is List<Question> questionList = new ArrayList<Question>();
* With various Question's added already with various isCorrect values.
*/
for (int i = 0; i < questionList.size(); i++) {
Question q = (Question) questionList.get(i);
q.setCorrect(true);
}
Will this set each Question’s isCorrect in the questionList to true? As I understand Java (and I don’t think I do) it should. But does it?
Thank you
Yes, it will. It’s the same object referenced in two places.