Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Basics
Packages
Topics To Be Covered
• Objective
• Packages
• Summary
Objectives
At the end of this course,
you will be able to understand:
• Package concept
• Packaging classes
Packages
Packages
• Packaging also help us to avoid class name
collision when we use the same class name as
that of others.
• For example, if we have a class name called
"Vector", its name would crash with the Vector
class from JDK. However, this never happens
because JDK uses java.util as a package name
for the Vector class (java.util.Vector ).
• Understanding the concept of a package will
also help us manage and use files stored in jar
files in more efficient ways
www.prolearninghub.com
Packages
• To use a package inside a Java source file, it is
convenient to import the classes from the
package with an import statement.
• import java.awt.event.*;
• The above statement imports all classes from
the java.awt.event package.
www.prolearninghub.com
Packages
• Why Packages?
– To avoid naming conflicts
– To control access
– To achieve Reusability
• What is a Package?
– A package is a group of related types (classes,
interfaces etc.) providing access and namespace
management
www.prolearninghub.com
Package Declaration
• Package declaration is file based;
–All classes in the same source file belong to the
same package.
–Each source file may contain an optional
package declaration in the following form.
Package packagename;
–Let us consider the source file
ElevatorFrame.java, for example.
Package elevator;
Public class ElevatorFrame
www.prolearninghub.com
Package Declaration
• The package declaration at the top of the source
file declares that the ElevatorFrame class belongs
to the package named elevator.
• When the package declaration is absent from a
file, all the classes contained in the file belong to
unnamed package.
www.prolearninghub.com
Packages
Predefined Packages
• Already defined in the Java API library and can be imported
into user programs
• Examples of predefined packages:
Core packages
java.lang.*;
Frequently required Operations
(By default, it is imported)
java.util.*;
Utility Operations
java.sql.*;
Database operations
java.net.*;
Networking operations
java.io.*;
File related operations
java.awt.*;
GUI operations
Extended Packages
javax.sql.*;
Database
javax. servlets.•; Servlets
www.prolearninghub.com
Packages
Predefined Packages
• A package allows logical grouping of classes, interfaces
package myPackage;
public class MyClass {
MyClass()
{
}
}
• Strict file & directory naming conventions and organization
• All classes put into a package must reside in a directory with
that package name
• Package name is strictly specified at the beginning of the
java code
www.prolearninghub.com
Packages
•
Importing Packages
To import a package in some program, import
keyword is used
import myPackage.*;
public class NewClass {
NewClass()
{
}
}
• Here import statement will import or bring all classes in
package myPackage
• To import built-in package we write,
• import java.io.*;
• import java.sql.*;
www.prolearninghub.com
Packages
Package Example
package myPack ;
public class ClassB {
public void method1( ) {
System.out.printIn("This is from ClassB Method1 message");
}
}
package myPack ;
public class ClassC {
public void method2( ) {
System.out.printIn("This is from ClassC Method2 message");
}
}
www.prolearninghub.com
Packages
Package Example
import myPack.*;
public class ClassA {
public static void main(String[] args) {
ClassB cb = new ClassB();
cb. methodl( );
ClassC cc=new ClassC();
method2();
System.out.println("Main class”);
}
}
www.prolearninghub.com
Packages
Working with CLASSPATH
•
An environment variable which tells the JVM and java compiler where
to look for class files
• Class files are searched in the directories specified in the class path in
the order specified
Adds the current classpath
• Set CLASSPATH-%CLASSPATH%; . ; source location from root
folder;
The symbol . (dot) means
Current Directory
• Example: set CLASSPATH=%classpath%;…
www.prolearninghub.com
Summary
Summary
In this session, we have covered:
Packages
• Introduction
• Predefined packages
• User defined package
www.prolearninghub.com