Reputation: 7908
I'm trying to learn java and seem to be missing something obvious.
In subdirectory lab I have the file Play.java
package lab;
import java.io.*;
public class Play {
public static void playprint(Object obj) {
System.out.println(obj);
}
}
My CLASSPATH starts with '.'
In the parent directory I have a program
public class test {
public static void main(String[] args) {
lab.Play.playprint("hello world");
}
}
This runs fine. If I change the program to
import lab.Play.*;
public class test {
public static void main(String[] args) {
playprint("hello world");
}
}
It fails with an error that it can't find symbol method playprint
What am I missing?
Upvotes: 1
Views: 157
Reputation: 1754
If you want to skip using static methods, you can create an object of Play
class inside the class test and then call playprint.
Upvotes: 1
Reputation: 4537
To import a method you have to use import static. Without you are trying to import all classes (and interfaces) within your class "Play" only.
import static lab.Play.*;
See the documentation on static imports for details.
Upvotes: 5