Skip to main content

Java For Python Programmers Edition 2

Section 9.3 Forgetting to declare your variables

In Python, you can use a variable without declaring it first, but in Java, you must declare all variables before using them. If you try to use a variable that has not been declared, the Java compiler will give you an error message like this:
The ’cannot find symbol’ error for the variable count on line 6 indicates that count was used before it was declared within the Histo class. In Java, all variables must be explicitly declared with a data type (e.g., int, String, ArrayList<Integer>) before they can be assigned a value or referenced in any way. The arrow in the error message points to where the undeclared variable count was first encountered. To resolve this, count needs to be declared with its appropriate type (e.g., ArrayList<Integer> count;) before any attempt to initialize or use it.
You have attempted of activities on this page.