Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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
                Doesn't look like that answered my question. Can you have a closer look at the current context and the question you "possibly" marked as duplicate?
– Swadhikar
                Jul 5, 2016 at 6:28
                Well if you read the answer and subsequent question tagged in the answer that should clear your doubt.
– Akash Lodha
                Jul 5, 2016 at 6:33
                And just for the record: using the default package is bad practice and really really not something you should do. And the names of classes should say what they "are"; and in that sense "Util" isn't exactly an example of great naming either.
– GhostCat again on strike
                Jul 5, 2016 at 6:39
                @Jägermeister: Valid points and agreed! I am trying to implement and learn some of the basics and practice. However I would keep it in mind while working with default package names. Cheers!
– Swadhikar
                Jul 5, 2016 at 6:41
                hi Rajesh, thanks for sharing.. I have done as you suggested. Now getting the below error..  .\ListOfNumbers.java:4: error: cannot find symbol import static Util.print;               ^   symbol: class Util .\ListOfNumbers.java:4: error: static import only from classes and interfaces
– Swadhikar
                Jul 5, 2016 at 6:23
                Have a package definition for your classes. static import from default package is not possible bugs.java.com/bugdatabase/view_bug.do?bug_id=4989710
– rajesh
                Jul 5, 2016 at 6:30