Answer the question
In order to leave comments, you need to log in
Why does one class not see the variables of another?
I am learning on the well-known JAVA site and I don’t understand why MainActivity doesn’t see the variables from Question.java
I know that the question is stupid, but I don’t understand myself (
Answer the question
In order to leave comments, you need to log in
Because:
1. A class is not just a file in which the code is written. Until you have created an object (an instance of a class), it does not exist, as well as its variables (with the exception of static variables).
2. To access variables of another class, you must access them through this class, for example:
Question question = new Question();
....
public boolean isCorrect(Question quest) {
return quest.correctAnswer == quest.playerAnswer;
}
Because you teach Java on a well-known site, and not from a textbook.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question