How to import a method from a package into another program? I don't know how to import... I write a lil' code:
package Dan;
public class Vik
{
public void disp()
{
System.out.println("Heyya!");
}
}
and then, saved it in a folder named "Dan" and I compiled it. The .class file is generated. Then, I wrote this code below:
import Dan.Vik.disp;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
and I saved it outside the folder "Dan" and it says : "cannot find symbol"
I saved the first code in C:\Dan\Vik.java and the second in C:\Kab.java
You should use
This makes the class visible and the its public methods available.
In Java you can only import class Names, or static methods/fields.
To import class use
to import static methods/fields use
Take out the method name from in your import statement. e.g.
becomes:
For the second class file, add "package Dan;" like the first one, so as to make sure they are in the same package; modify "import Dan.Vik.disp;" to be "import Dan.Vik;"
You don't import methods in Java, only types:
The exception is so-called "static imports", which let you import class (
static
) methods from other types.In Java you can only import class Names, or static methods/fields.
To import class use
We can also import static methods/fields in Java and this is how to import