Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Basics Packages.

Similar presentations


Presentation on theme: "Java Basics Packages."— Presentation transcript:

1 Java Basics Packages

2 Topics To Be Covered Objective Packages Summary

3 Objectives Package concept Packaging classes
At the end of this course, you will be able to understand: Package concept Packaging classes

4 Packages

5 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

6 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.

7 Packages Why Packages? What is a Package? 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

8 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 { public double x; //……..}

9 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.

10 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

11 Packages Predefined Packages
A package allows logical grouping of classes, interfaces 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 package myPackage; public class MyClass { MyClass() { }

12 Packages Importing Packages
To import a package in some program, import keyword is used 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.*; import myPackage.*; public class NewClass { NewClass() { }

13 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"); }

14 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”); }

15 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 Set CLASSPATH-%CLASSPATH%; . ; source location from root folder; Example: set CLASSPATH=%classpath%;… Adds the current classpath The symbol . (dot) means Current Directory

16 Summary

17 Summary In this session, we have covered: Packages Introduction
Predefined packages User defined package


Download ppt "Java Basics Packages."

Similar presentations


Ads by Google