Packages in Java.

In this article,Packages in Java is explained in detail with examples.
A package is the collection of Java programs in compiled format(.class files) grouped together as a single unit.
How project delivered to the client using packages in Java.
  • Develop java program.
  • compile the programs.
  • .classfiles.
  • All class files are grouped together in a folder directory known as PACKAGE.
  • This package is compressed using .Jar file..
  • .Jar file is delivered to clients.
The package is nothing but a directory/folder in our system.
A package provides security to our classes and its members using access modifiers (Private, default/ protected/public), for accessing outside the folder or directory.(From topic Packages in Java)
Creating a Package
Package is a keyword using which we create a package in a java program.
Syntax
package(package-name);
While creating a package make sure "package name" is same that of "directory or folder name".
After creating the package with some java class to use such packages from another folder, we use "import statement" using import keyword.
Ex
import(package name) .*;
* indicates JVM to use all the contents or class of that package.(From topic Packages in Java.)
Ex
program java1;
public class Father
{
public void fathermethod()
{
System.out.println("Hello Father");
Mother m=new Mother();
m.MotherMethod();
}
}
class Mother
{
void motherMethod()
{
System.out.println("Hello mother");
}
}
package java2; 
public class Son
{
public void SonMethod()
{
System.out.println("Hello Son");
Daughter d=new Daughter();
d.daughterMethod();
}
}
class Daughter
{
void daughterMethod()
{
System.out.println("Hello Daughter");
}
}
import java1.*;
import java2.*;
class package1
{
public static void main(String args[])
{
Father f=new Father();
f.fatherMethod();
Son S=new Son();
S.SonMethod();
Mother m=new Mother();
m.methodMethod();
Daughter d=new Daughter();
d.daughterMethod();
}
}
Note
In the above program(package1-class),We can't access mother and daughter classes because they are default classes in java1 & java2 packages. Hence such classes are used/accessible with in the package.i.e, We use mother class in father class and daughter class in son-class respectively.
Adding a new class to a package
To add a new class for an existing package,create a new program with a new class.Using package-name.(From topic Packages in Java)
Ex
public class Newclass
{
public void newMethod()
{
Syste.out.println("Hello new method of new class");
}
}
package1.java
import java1.*;
import java2.*;
class package1
{
public static void main(String args[])
{
-----
-----
//using new-class
Newclass nc=new Newclass();
nc=newMethod();
Note
Make and Built utility
JVM dynamically detects changes done to dependant class of your correct program,such classes are compiled first for new-changes and then your current program compiled.This utility is known as make and built utility.
Whenever a package is getting imported in a java program,JVM first searches for such packages in your current working directory.If not available then we get an error.
classpath is the variable used by JVM for searching imported package in your program.It is used as follows,
Getting the class path 
We use the following command
C:\java>set classpath=%classpath%;d:\java;
C:\java>echo%classpath%
Ex
import java1.*;
import java2.*;
class package2
{
public static void main(String args[])
{
----
----
----
}
}
Note
The above classpath set using command prompt is temporary, we can make it permanent by setting classpath in system environment variables.
Steps:
Click start--Right click on my computer--select properties--select Advanced setting--click environment variables button--under user variables. create your "classpath".
Sub-package Creating an another package inside a particular package is known as sub-package.
Ex
package java1.java11;
public class subpackage
{
public void subpackageMethod()
{
System.out.println("Sub-package () of subpackage class");
}
}
import java1.*;
import java2.*;
import java1.java11.*; //sub-package import
class package2
{
public static void main(String args[])
{
Father f=new Father();
f.FatherMethod();
----
----
---
---
Subpackage sp=new Subpackage();
sp.SubpackageMethod();
}
}

Static import in java packages:
This feature was introduced in java5, using this we can import only static contents of a class directly in your program and use them directly without class-name or object name in a program.
package java1.java11;
public class StaticImport
{
public static float interest1=10.75f; //homeloan
public static float interest2=13.75f; //personal loan
public static float interest3=6.75f; //car loan
public static void hellomethod()
{
System.out.println("Hello Static method user");
}
}
Package2.java
import java1.*;
import java2.*;
import java1.java11.*;
import static java1.java11.staticImport.*; //static import
class package2
{
public static void main(String args[])
{
----
----
---
//using class-name
System.out.println("Homeloan ROI:::"+staticImport.interest1);
System.out.println("Personalloan ROI:::"+staticImport.interest2);
System.out.println("Carloan ROI:::"+staticImport.interest3);
StaticImport.helloMethod();
//Using without class name directly
System.out.println("Homeloan ROI:::"+staticImport.interest1);
System.out.println("Personalloan ROI:::"+staticImport.interest2);
System.out.println("Carloan ROI:::"+staticImport.interest3);
helloMethod();
}
}
JAR File
JAR stands for "JAVA-Archieve-File".It is the single file,compressed by a small tool provided by JAVA software.
This .jar file is used to group together all packages sub packages and its .classfiles as single unit or file
It is created using following commands.
Syntax:
jar-cvf(filename.jar)(pkgs&classes)
jar-is a command
-c->is option for creating new jar file
-v->is option for verbose(% of comparision)
-f->is option to provide new jar-file name.
-x->is option for extracting jar file
the command goes as follows
C:\Users\PHA>d:
D:\>cd java
D:\java>jar-cvf(lib.jar java1\*.class java1/java11/*.class java2\*.class.
Now this lib.jar is kept in class-path as used in your projects as library
C:\java>set classpath=d:\java\lib.jar;
C:\java>echo%class path%
Extracting jar file
D:\java\myfolder>jar -xf lib.jar
All packages in java and its class files are extracted in your current folder only.

Continue to next topic Life cycle of a thread.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprisessurprise planners Hyderabad..

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required