Program that illustrates the Creation of simple package.
//Let's say we have two classes in a package named mypackage:
//Welcome.java (located in the mypackage package):
package mypackage;
public class Welcome
{
public void greet()
{
System.out.println("Welcome to my package!");
}
}
//Main.java (located outside the mypackage package):
import mypackage.Welcome;
public class Main
{
public static void main(String[] args)
{
Welcome welcome = new Welcome();
welcome.greet();
}
}
/*To create this package and the classes, follow these steps:
1) Create a directory named mypackage to contain your package's classes.
2) Inside the mypackage directory, create a file named Welcome.java and add the contents of the first code snippet.
3) Outside the mypackage directory, create a file named Main.java and add the contents of the second code snippet.
4) Open a terminal/command prompt and navigate to the parent directory that contains both the mypackage directory and the Main.java file.
5) Compile the classes. First, compile the Welcome.java file in the mypackage directory:
javac mypackage/Welcome.java
6) Then, compile the Main.java file in the parent directory:
javac Main.java
7) Run the program:
java Main
You should see the output:
Welcome to my package!
*/


0 Comments