Professional Documents
Culture Documents
Module II - Packages in Java
Module II - Packages in Java
Module II
Package in Java
Presented by: Dr. Ram Paul Hathwal
We can turn these into objects containing data and methods.
We can also set them as static – meaning we can use the methods without making an
object.
Review Department of Computer
Science and Engineering
Subclasses invisibly pick up all the superclass code and can use the public bits.
If a method expects a parent type, it can take a subclass in, but can only use parent
methods and variables.
Java Packages Department of Computer
Science and Engineering
If a source file does not begin with the package statement, the classes
contained in the source file reside in the default package.
Example:
import static java.lang.Math.*;
...
double angle = sin(PI / 2) + ln(E * E);
Static import allows you to refer to the members of another class without writing
that class's name.
Should be used rarely and only with classes whose contents are entirely static
"utility" code.
Referring to Packages Department of Computer
Science and Engineering
packageName.className
Example:
java.util.Scanner console =
new java.util.Scanner(java.lang.System.in);
You can use a type from any package without importing it if you write its full name.
Sometimes this is useful to disambiguate similar names.
◦ Example: java.awt.List and java.util.List
◦ Or, explicitly import one of the classes.
The default Package Department of Computer
Science and Engineering
Compilation units (files) that do not declare a package are put into a default,
unnamed, package.
Classes in the default package:
Cannot be imported
Cannot be used by classes in other packages
Defining a Package:
This statement should be used in the beginning of the program to include that program
in that particular package.
package <package name>;
The package keyword is used to create a package in java.
To Compile: javac -d directory JavaFileName.java
javac -d C:/javaprograms JavaPackages.java
To Run: java packageName.ClassName
java javaprograms.JavaPackages
The -d switch specifies the destination where to put the generated class file
If you want to keep the package within the same directory, you can use . (dot).
Package Access Control Department of Computer
Science and Engineering
JAVA uses file system to Package names are case The classes declared
manage packages, with sensitive. within the package
each package stored in its The directory name and directory will belong to
own directory. the package name should the specified package.
be the same.
Department of Computer
Steps to create Package Science and Engineering
Every Java source file can contain only class declared as public.
The name of the source file should be same as the name of the public class
with .java extension.
To put a class into a package, one uses the "package" statement:
The package statement MUST be the first line of code within the file.
It can be proceeded by comments.
import p1.ClassA;
package p1;
Class testclass
public class ClassA
{
{
public static void main(String str[])
public void displayA( )
{ {
System.out.println(“Class A”); ClassA obA=new ClassA();
} obA.displayA();
} }
}
Source file – ClassA.java
Subdirectory-p1 Source file-testclass.java
import p2.ClassB;
class PackageTest3
//MyCalculator.java //TestPackage.java
package p1;
import p1.MyCalculator;
public class MyCalculator{
class TestPackage{
public int Add(int x,int y)
{ public static void main(String[] args)
return x+y; {
} MyCalculator M = new MyCalculator();
public int Subtract(int x,int y)
System.out.print("\n\n\tThe Sum is : " + M.Add(45,10));
{
return x-y;
System.out.print("\n\n\tThe Subtract is :" +
M.Subtract(45,10));
}
public int Product(int x,int y) System.out.print("\n\n\tThe Product is :" +
{
M.Product(45,10));
return x*y; }
}} }
Package exercise Department of Computer
Science and Engineering
The "root" directory of the package hierarchy is determined by your class path or the
directory from which Java was run.We set the environmental variable ‘CLASSPATH’.
Environmental variables are just variables all computers keep which store user preferences.
You can tell the ‘javac’ and ‘java’ programs the classpath using…
set CLASSPATH=.;C:\
The CLASSPATH is a variable that contains a list of directories which are separated by
colons (:) and under the Windows OS, they are separated by semicolons (;)
The programs will look in any directory listed in the CLASSPATH for the start of
directory trees. One path should be “.” if you want to include the present directory.
Department of Computer
CLASSPATH Science and Engineering
Note that if your code is unpackaged this can become quite complicated, as the
classpath must include the directory your code is running from, and the root of the
hierarchy.
Assuming you are in the latter, you'll need this kind of thing:
javac unpackaged/lazyCode/*.java
java -classpath unpackaged/lazyCode/;. MyClass
We can have only one class in Package to be public , and other than
public are not accessible outside so what to do.
We have to create different files within same package for each public
class make different files.
Please note that source file and class file of all the files of package
should be in package only and not in root directory.
Files can be in different directory then to deal with them CLASS PATH
come in action.
If you import a package, all the classes and interface of that package will
beimported excluding the classes and interfaces of the subpackages.
Hence, you need to import the subpackage as well. 28
Department of Computer
Science and Engineering
End
Thank you…