Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

Java Package

Uploaded by

sijedan722
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java Package

Uploaded by

sijedan722
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Java

Packages
Packages
• PACKAGE in Java is a collection of classes, sub-
packages, and interfaces. It helps organize your classes into
a folder structure and make it easy to locate and use them.
• Note: it helps improve code reusability.
• Each package in Java has its unique name and organizes its
classes and interfaces into a separate namespace, or name
group.
• Note: interfaces and classes with the same name cannot
appear in the same package, they can appear in different
packages. This is possible by assigning a separate
namespace to each Java package.
Packages
• Package in java can be categorized in two form, Java API-
Packages or built-in package and user-defined package.
• There are many built-in packages such as java, lang, awt,
javax, swing, net, io, util, sql etc.
• Here, we will have the detailed learning of creating and
using user-defined packages.
Packages
• Java provides a large number of classes
grouped into different packages based on a
particular functionality.
• Examples:
• java.lang: It contains classes for primitive
types, strings, math functions, threads, and
exceptions.
• java.util: It contains classes such as vectors,
hash tables, dates, Calendars, etc.
• java.io: It has stream classes for Input/Output.
• java.awt: Classes for implementing Graphical
User Interface – windows, buttons, menus,
etc.
• java.net: Classes for networking
• java. Applet: Classes for creating and
implementing applets
Advantages of using Packages in Java
• Make easy searching or locating of classes and interfaces.
• Avoid naming conflicts. For example, there can be two classes
with the name Java1 in two packages, university.csdept.Java1 and
college.itdept.Java1
• Implement data encapsulation (or data-hiding).
• Provide controlled access: The access specifiers protected and
default have access control on package level. A member declared
as protected is accessible by classes within the same package and
its subclasses. A member without any access specifier that is
default specifier is accessible only by classes in the same package.
• Reuse the classes contained in the packages of other programs.
• Uniquely compare the classes in other packages.
Syntax:
• package nameOfPackage;
• When compiling a Java class that is stored in a
package, you must include the path that corresponds
to the package. You will learn how to compile a Java
class into a package and run a Java program to test
your work by following these nine steps.
How to Create a package?
• Create a new folder called javafolder.
• Create a subfolder in your new folder called javapackage.
• Open your text editor and create a new file that will contain
the Java1 class in the javapackage. Be sure to create this file
in the javapackage folder. Type in the following Java
statements:
• Note the package statement; this statement tells the
compiler that the class is contained within the package
(i.e., folder) named javapackage. Therefore, the actual
name of your Java class is javapackage.Java1.
• Now Save your file as Java1.java.
Continued..
• Now In your text editor, create the Java
program that will test the Java1 class. Be sure
to save this file in the java folder. Type in the
following Java statements:
– Here the import statement directs the compiler to
resolve references to Java1 from
the javapackage package.
– Or javac -d directory javaFileName
Continued….
• Save your file as TestJavaPackage.java.
• Now, open a command prompt and navigate to the java directory. Then type in
the command(javac ./javapackage/Java1.java) or(javac -d directory
javaFileName) to compile the Java1 source and hit Enter. Notice that you
provided the path of the Java file. This path corresponds to the package name.
– Here, -d specifies the destination where to locate the generated class file. You can use any
directory name like /home (in case of Linux), C:/folderName (in case of windows), etc. If
you want the package to be present in the same directory, you can use the dot ( . ).
• In the same directory, type in the command( javac TestJavaPackage.java) to
compile the tester class and hit Enter.
• Now you are ready test your Java1 class that is stored in javapackage. Type in
the command(java TestJavaPackage) to run the Java runtime launcher and
hit Enter. Observe the output showing the Java1 you instantiated
using javapackage.Java1.
Java1 Package
• package javapackage;
• public class Java1
• {
• private String fName;
• private String lName;
• public String getFName()
• {
• return fName;
• }
• public void setFName(String fName)
• {
• this.fName=fName;
• }
• public String getLName()
• {
• return lName;
• }
• public void setLName(String lName)
• {
• this.lName=lName;
• }
• }
TestJavaPackage.java
• import javapackage.Java1;
• public class TestJavaPackage
• {
• public static void main(String[] args)
• {
• Java1 j=new Java1();
• j.setFName("Annu");
• j.setLName("Mehla");
• System.out.println("Student Data:"+j.getFName()+"
"+j.getLName());
• }
• }
Thank You !

You might also like