In Java, a package is a container for a collection of classes, sub-packages, and interfaces. The package keyword is used to create a package in java.
Packages are used for the following purposes:
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to Coderzpy package");
}
}
What is the procedure for compiling a Java package?
If you are not using an IDE, you must use the following syntax:
javac -d directory javafilename
As an example,
javac -d . Simple. java
The -d switch indicates where the created class file should be saved. You can use any directory name, such as /home (in Linux), d:/abc (in Windows), and so on. You can use it if you wish to keep the package in the same directory (dot).
To run the java package program:
To Compile: javac -d . Simple.java
To Run: java mypack.Simple
Output:
Welcome to Coderzpy package
There are three ways to get into the package from the outside.
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello Coderzpy");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Hello Coderzpy
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello Coderzpy");}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Hello Coderzpy
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello Coderzpy");}
}
//save by B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}
Hello Coderzpy
These packages contain a huge number of classes that are included in the Java API. The following are some of the most often used built-in packages:
These are the packages that the user has defined. Refer to the above-given examples with the package pack and it’s class A.
Note: also read about the instanceof Operator in Java
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…