3

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
Swadhikar
  • 2,152
  • 1
  • 19
  • 32
  • 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 05 '16 at 06:28
  • Well if you read the answer and subsequent question tagged in the answer that should clear your doubt. – Akash Lodha Jul 05 '16 at 06:33
  • 1
    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 Jul 05 '16 at 06: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 05 '16 at 06:41
  • Possible duplicate of http://stackoverflow.com/questions/11782076/static-import-only-from-classes-and-interfaces – Evandro Coan May 05 '17 at 19:01

2 Answers2

3

You should import the method name

import static Util.print;

If you want to import all static methods from Util:

import static Util.*;
rajesh
  • 3,247
  • 5
  • 31
  • 56
  • 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 05 '16 at 06:23
  • Have a package definition for your classes. static import from default package is not possible http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4989710 – rajesh Jul 05 '16 at 06:30
1

The below answer helped me to overcome the problem! Guess this must be a bug.

Java static member from default package cannot be imported!

Community
  • 1
  • 1
Swadhikar
  • 2,152
  • 1
  • 19
  • 32