Reputation: 45
Is there a way to use the standard library class Scanner without an import statement?
My professor is the type to include overly difficult problems that you are not expected to actually complete but aside from decompiling the standard library and adding all of the .java files to the project I am at a bit of a loss.
Edit: to clarify, this is the exact wording of the lab description: "Use a scanner to get an integer value from the user. Your program may not use any import statements."
Upvotes: 1
Views: 3195
Reputation: 22997
Yes. You can fully qualify them on usage:
java.util.Scanner scanner = new java.util.Scanner(System.in);
The import statement is not about the compiler asking where can I find the class?, but rather which class do you mean?
With the import statement you say to the compiler: "Everytime I say Scanner
, I actually mean java.util.Scanner
. So please save me some typing.
So indeed, as Andreas already mentioned in the comments. import
is not necessary, it's just convenience.
Also note that all classes from the java.lang
package (for example System
in the abovementioned example) are implicitly imported (JLS § 7.3), so you can always use them without an import statement.
Upvotes: 4