Example : An Algorithm on how to create and use a Package in a Java program.
Step1: First of all we create a new directory/folder with a name say "mypackage" in any folder/drive.
Step2: Now, inside the "mypackage" directory, save/create a new Java file with certain program with a name say "MyClass.java" with the following content/codes:-
package mypackage;
public class MyClass
{
public void printMessage()
{
System.out.println("Hello, World!");
}
}
To create a package, we simply need to include the package keyword at the beginning of our Java file (package mypackage).
Step3: Now, save the file and compile it using the following command from the command line:-
javac mypackage/MyClass.java
After successful compilation(when a program is error free), a compiled file/class file will be created with a class name called "MyClass.class" inside the "mypackage" directory.
Step4: Now we can use the created class file("MyClass.class") of the created package say "mypackage" in another fresh Java programs using import keyword. The another fresh program of java to use the package is -
import mypackage.MyClass;
class MyFreshProgram
{
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.printMessage();
}
}
To use the created package in another fresh Java program, we'll need to import it at the beginning of the file using the package name as in above program (mypackage.MyClass)
Step5: Now, save the package import fresh java program file as "MyFreshProgram.java" and compile it using the following command from the command line:
javac MyFreshProgram.java
Step6: Now we can run the package import fresh program using the following command from the command line:
java MyFreshProgram
This will output "Hello, World!" to the console which confirms the package import.
Example : A Java program to Create a new Package and its use in another Java program.
//Package Creation
package mypackage;
public class MyClass
{
public void printMessage()
{
System.out.println("Hello, World!");
}
}
[Save as 'MyClass.java' file in 'mypackage' folder]
Only compile: (i) javac MyClass.java (If no error, 'MyClass.class' file is created in 'mypackage' folder.)
(ii) D:\>javac -d . MyClass.java [Here, MyClass.java file is saved in D drive and when compile creates a package name folder 'mypackage' automatically in D drive with 'MyClass.class' file inside it.]
----------------------------------------------------
// Import Created Package
import mypackage.MyClass;
class MyFreshProgram
{
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.printMessage();
}
}
[Save as 'MyFreshProgram.java' in outside 'mypackage' folder.]
Compile: javac MyFreshProgram.java
Run: java MyFreshProgram
Output: Hello World!
Example : A Java program to Create a new Package and use package methods in another Java program.
//Package Creation
package mypackage;
public class MyClass
{
int num1,num2,result;
public void input(int x, int y)
{
num1=x;
num2=y;
}
public void process()
{
result=num1+num2;
}
public void output()
{
System.out.println("The addition result is = "+result);
}
}
----------------------------------------------------
// Import Created Package
import mypackage.MyClass;
class MyFreshProgram
{
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.input(100,200);
obj.process();
obj.output();
}
}
Output:
D:\>javac Program.java
D:\>java MyFreshProgram
The addition result is = 300
NB: Follow tha required assumptions as above example.
Example : A Java program to Create a new Package and use package methods with return type in another Java program.
package mypackage;
public class MyClass
{
int num1,num2,result;
public void input(int x, int y)
{
num1=x;
num2=y;
}
public int process()
{
result=num1+num2;
return result;
}
}
----------------------------------------------------
import mypackage.MyClass;
class MyFreshProgram
{
public static void main(String[] args)
{
int result;
MyClass obj = new MyClass();
obj.input(100,200);
result=obj.process();
System.out.println(result);
}
}
Output:
D:\>javac Program.java
D:\>java MyFreshProgram
300
Example : A Java program to Create a new Package with two classes and use both class methods in another Java program.
package mypackage;
public class MyClass
{
public void output()
{
System.out.println("Hello India");
}
}
Save as = MyClass.java
---------------------------------------------
package mypackage;
public class MyClass1
{
public void output()
{
System.out.println("Hello World");
}
}
Save as = MyClass1.java
[NB: Both MyClass.java and MyClass1.java are stored in same folder 'mypackage' in D drive.]
==============================================
import mypackage.MyClass;
import mypackage.MyClass1;
class MyFreshProgram
{
public static void main(String[] args)
{
MyClass obj1 = new MyClass();
MyClass1 obj2 = new MyClass1();
obj1.output();
obj2.output();
}
}
Save as = 'Program.java' in D drive.
Output:
D:\mypackage>javac MyClass1.java
D:\mypackage>javac MyClass.java
D:\mypackage>cd\
D:\>javac Program.java
D:\>java MyFreshProgram
Hello India
Hello World
0 Comments