I am trying to cut short the System.out.println()
by defining a static method inside Util.java
. I would like to make use of Util.print() further shortened into print()
. So I have did a static import.
Util.java is present under the same directory as ListOfNumbers.java. When I try to access writeList() from a tester class I get the below error:
import java.io.*;
import java.util.List;
import java.util.ArrayList;
import static Util;
public class ListOfNumbers {
...
...
public void writeList() {
// The FileWriter constructor throws IOException, which must be caught.
PrintWriter out = null;
try {
print("Entered try statement...");
...
} catch (IOException | IndexOutOfBoundsException e){
print("Exception thrown: \n" + e.getMessage());
...
}
}
}
Error:
>> javac Tester.java && java Tester
.\ListOfNumbers.java:4: error: '.' expected
import static Util;
^
.\ListOfNumbers.java:4: error: ';' expected
import static Util;
^
.\ListOfNumbers.java:4: error: cannot find symbol
import static Util;
^
symbol: class Util
.\ListOfNumbers.java:4: error: static import only from classes and interfaces
import static Util;
^
.\ListOfNumbers.java:24: error: cannot find symbol
print("Entered try statement...");
^
symbol: method print(String)
location: class ListOfNumbers
.\ListOfNumbers.java:34: error: cannot find symbol
print("Exception thrown: \n" + e.getMessage());
^
symbol: method print(String)
location: class ListOfNumbers
6 errors