How to create package in Java
a. Create a directory:
First, create a directory (e.g., pack1) with the name of the package.
b. Create a Java file:
Create a Java file (e.g., [Link]) inside the newly created directory.
c. Specify the package name:
In the Java file, specify the package name using the package keyword.
d. Save the file correctly:
Save the file with the same name as the public class.
e. Class visibility rule:
Note: Only one class in a program can be declared as public.
f. Use the package in another program:
You can use this package in another Java program (e.g., [Link]) by importing it with import
pack.*.
Example of package that import the packagename.*
//save by [Link] inside “pack” folder
package pack1;
public class A
{
public void msg()
{
[Link]("Hello");
}
}
//save by [Link]
import pack1.*;
class B
{
public static void main(String args[])
{
A obj = new A();
[Link]();
}
}