Skip to main content

Java For Python Programmers Edition 2

Section 9.5 Forgetting to use the new keyword to create an object

Unlike Python, where you can create a new object without explicitly using a keyword, Java requires the new keyword to instantiate a new object.
This error message occurs when you forget to use the new keyword to instantiate an object. Specifically, on line 8 of Histo.java, data = Scanner(new File("test.dat")); leads to a ’cannot find symbol’ error. While the message states ’symbol: method Scanner(File)’, this can be misleading. Java incorrectly interprets Scanner() as an attempt to call a static method named Scanner within the Histo class (or an inherited one). However, Scanner is a constructor of the Scanner class, not a standalone method, and therefore requires the new keyword (e.g., data = new Scanner(new File("test.dat"));) to create a new Scanner object.
You have attempted of activities on this page.