Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sampath Kumar S Assistant Professor, SECE

Similar presentations


Presentation on theme: "Sampath Kumar S Assistant Professor, SECE"— Presentation transcript:

1 Sampath Kumar S Assistant Professor, SECE
Packages Sampath Kumar S Assistant Professor, SECE

2 12/31/2018 Packages A package is a group of similar types of classes, interfaces and sub-packages. (Or) A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management. Package can be categorized in two form, built-in package (java, lang, awt, javax, swing, net, io, util, sql etc) user-defined package. The package keyword is used to create a package. Sampath Kumar S, AP

3 12/31/2018 Advantage of Package? Package is used to categorize the classes and interfaces so that they can be easily maintained. Package provides access protection. Package removes naming collision. Sampath Kumar S, AP

4 12/31/2018 Example of package //save as Simple.java package mypack; public class Simple{ public static void main(String args[]){ System.out.println("Welcome to package"); } } Sampath Kumar S, AP

5 12/31/2018 Compiling the Package If you are not using any IDE, you need to follow the syntax given below: javac -d directory <javafilename> Example:   javac -d . Simple.java  Note:  The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot). Sampath Kumar S, AP

6 Run the Package To Compile: javac -d . Simple.java
12/31/2018 Run the Package To Compile: javac -d . Simple.java To Run: java mypack.Simple Note: The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The .(dot) represents the current folder. We need to use fully qualified name e.g: mypack.Simple to run the class Sampath Kumar S, AP

7 Accessing package from another package
12/31/2018 Accessing package from another package There are three ways to access the package from outside the package. import package.*; import package.classname; fully qualified name. Sampath Kumar S, AP/IT

8 1. Using packagename.* 12/31/2018 If we use “package.*” then all the classes and interfaces of this package will be accessible but not subpackages. The import keyword is used to make the classes and interface of another package accessible to the current package. Sampath Kumar S, AP

9 Example: packagename.*
12/31/2018 Example: packagename.* //save by A.java   package pack;   public class A {     public void msg(){ System.out.println("Hello"); }   Sampath Kumar S, AP

10 Example: Cont.., Output: Hello //save by B.java package mypack;
12/31/2018 Example: Cont.., //save by B.java   package mypack;   import pack.*;      class B{     public static void main(String args[]){      A obj = new A();      obj.msg();     }   }   Output: Hello Sampath Kumar S, AP

11 2. Using Packagename.classname
12/31/2018 If we import “package.classname” then only declared class of this package will be accessible. Sampath Kumar S, AP

12 Example: Packagename.classname
12/31/2018 Example: Packagename.classname //save by A.java   package pack;   public class A {     public void msg(){ System.out.println("Hello"); }   Sampath Kumar S, AP

13 Example: Cont.., Output: Hello //save by B.java package mypack;
12/31/2018 Example: Cont.., //save by B.java   package mypack;   import pack.A;      class B{     public static void main(String args[]){      A obj = new A();      obj.msg();     }   }   Output: Hello Sampath Kumar S, AP

14 3. Using fully qualified name
12/31/2018 3. Using fully qualified name If we use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But we need to use fully qualified name every time when we are accessing the class or interface. It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class. Sampath Kumar S, AP

15 Example: Packagename.classname
12/31/2018 Example: Packagename.classname //save by A.java   package pack;   public class A {     public void msg(){ System.out.println("Hello"); }   Sampath Kumar S, AP

16 Example: Cont.., Output: Hello //using fully qualified name
12/31/2018 Example: Cont.., //save by B.java   package mypack;   import pack.A;      class B{     public static void main(String args[]){       pack.A obj = new pack.A(); //using fully qualified name         obj.msg();     }   }   Output: Hello Sampath Kumar S, AP

17 Note: If we import a package, subpackages will not be imported.
12/31/2018 Note: If we import a package, subpackages will not be imported. If we import a package, all the classes and interface of that package will be imported excluding the classes and interfaces of the subpackages. Hence, we need to import the subpackage as well. Sampath Kumar S, AP

18 12/31/2018 Subpackage Package inside the package is called the subpackage. It should be created to categorize the package further. The standard of defining package is domain.company.package e.g. in.sece.it Sampath Kumar S, AP

19 Example: Output: IT a Subpackage of SECE
12/31/2018 Example: package in.sece.it; class Simple{ public static void main(String args[]){ System.out.println(“IT a Subpackage of SECE"); } } Note: To Compile: javac -d . Simple.java To Run: java in.sece.it.Simple Output: IT a Subpackage of SECE Sampath Kumar S, AP

20 Ways to load the class files or jar files
12/31/2018 Ways to load the class files or jar files There are two ways to load the class files temporary and permanent. Temporary By setting the classpath in the command prompt By -classpath switch Permanent By setting the classpath in the environment variables By creating the jar file, that contains all the class files, and copying the jar file in the jre/lib/ext folder. Sampath Kumar S, AP

21 Send the class file to another directory or drive
12/31/2018 Send the class file to another directory or drive If want to put the class file of A.java source file in classes folder of c: drive Sampath Kumar S, AP

22 12/31/2018 Example: //save as Simple.java package mypack; public class Simple{ public static void main(String args[]){ System.out.println("Welcome to package"); } } Sampath Kumar S, AP

23 12/31/2018 Cont.., To Compile: e:\sources> javac -d c:\classes Simple.java To Run: (Method 1) To run this program from e:\source directory, we need to set classpath of the directory where the class file resides. e:\sources> set classpath=c:\classes;.; e:\sources> java mypack.Simple Sampath Kumar S, AP

24 12/31/2018 Cont.., To Run: (Method 2) Another way to run this program by “-classpath” switch of java. The “-classpath” switch can be used with javac and java tool. To run this program from “e:\source“ directory, we can use “-classpath” switch of java that tells where to look for class file. For example: e:\sources> java -classpath c:\classes mypack.Simple Sampath Kumar S, AP

25 Ways to load the class files or jar files
12/31/2018 Ways to load the class files or jar files There are two ways to load the class files temporary and permanent. Temporary By setting the classpath in the command prompt By -classpath switch Permanent By setting the classpath in the environment variables By creating the jar file, that contains all the class files, and copying the jar file in the jre/lib/ext folder. Sampath Kumar S, AP

26 12/31/2018 Sampath Kumar S, AP

27 12/31/2018 Thank You  Sampath Kumar S, AP


Download ppt "Sampath Kumar S Assistant Professor, SECE"

Similar presentations


Ads by Google