0% found this document useful (0 votes)
1 views2 pages

How To Create Package in Java

To create a package in Java, first create a directory named after the package, then create a Java file within that directory and specify the package name using the package keyword. Ensure the file is saved with the same name as the public class, and remember that only one class can be declared as public in a program. You can use the package in another program by importing it with the import statement.

Uploaded by

Diya Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

How To Create Package in Java

To create a package in Java, first create a directory named after the package, then create a Java file within that directory and specify the package name using the package keyword. Ensure the file is saved with the same name as the public class, and remember that only one class can be declared as public in a program. You can use the package in another program by importing it with the import statement.

Uploaded by

Diya Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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., A.java) 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., B.java) by importing it with import
pack.*.

Example of package that import the packagename.*

//save by A.java inside “pack” folder


package pack1;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}

//save by B.java
import pack1.*;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}

You might also like