These packages include numerous classes that are a part of the Java API.
These are the packages the user has defined. First, we create the HELLO directory (name should be same as the name of the package). After that, create the Encapsulation inside the directory with the package names as the first statement.
// Java Packages example
package HELLO; class Encapsulation{ private String EmpName; private int EmpId; public String getEmpName() { return EmpName; } public void setEmpName(String empName) { EmpName = empName; } public int getEmpId() { return EmpId; } public void setEmpId(int i) { EmpId = i; } } class Main { public static void main(String[] args){ Encapsulation encapsulation = new Encapsulation(); encapsulation.setEmpName("Mohan"); encapsulation.setEmpId(421658); System.out.println("The Name of the Emp is : " +encapsulation.getEmpName() +" & EmpID is : "+encapsulation.getEmpId()); } }
The best way to run and compile a Java package without using an IDE
Syntax
To Compile: javac -d . Main.java
To Run: java HELLO.Main
The destination for the generated class file is specified by the -d switch. You are free to use any directory name, such as c:/example. Use if you want to maintain the package in the same directory (dot).
Output:
The Name of the Emp is : Mohan & EmpID is : 421658
//PackageCheck.java
package tutorial; public class PackageCheck{ public void Print(){ System.out.println("Hello World"); } }
//Main.java
package HELLO; import tutorial.*; class Main { public static void main(String[] args){ PackageCheck packageCheck=new PackageCheck(); packageCheck.Print(); } }
Output:
Hello World
//PackageCheck.java
package tutorial; public class PackageCheck{ public void Print(){ System.out.println("Hello World"); } }
//Main.java
package HELLO; import tutorial.PackageCheck; class Main { public static void main(String[] args){ PackageCheck packageCheck=new PackageCheck(); packageCheck.Print(); } }
Output:
Hello World
When you import a package, all of its classes and interfaces are imported except subpackages. So, you also need to import the subpackage.
Only this package's declared classes will be accessible if you use the fully qualified name. There is no longer a need to import. When utilizing a class or interface, however, you must always use the fully qualified name.When two packages belong to the same class, it is commonly used.
//PackageCheck.java
package tutorial; public class PackageCheck{ public void Print(){ System.out.println("Hello World"); } }
//Main.java
package HELLO; class Main { public static void main(String[] args){ tutorial.PackageCheck packageCheck=new tutorial.PackageCheck(); packageCheck.Print(); } }
Output:
Hello World
The Java programming language (versions 5 and above) introduced the concept of static import, which enables members (fields and methods) defined in a class as public static to be used in Java code without mentioning the class in which they are defined.
//PackageCheck.java
package HELLO; import static java.lang.System.*; class Main { public static void main(String[] args){ out.println("DockerTpoint"); } }
Output:
DockerTpoint
Post your comment