Java Package Advantage of Java Package

Slides:



Advertisements
Similar presentations
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Advertisements

Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Packages Sudhir Talasila Preeti Navale. Introduction Packages are nothing more than the way we organize files into different directories according to.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and.
How to Create a Java program CS115 Fall George Koutsogiannakis.
Introduction to Java Programming, 4E
1 Lecture 2 Java Packages  What are Java packages?  Why do wee need packages?  How to create a Java package?  Some examples.
Slide 1 of 40. Lecture A The Java Programming Language Invented 1995 by James Gosling at Sun Microsystems. Based on previous languages: C, C++, Objective-C,
Unit2: Object-oriented programming Getting started with Java Jin Sa.
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Welcome to Java. 2  History of java  Advantages of java  Java first program  Rules to be followed  Data types  Operators  Casting  Control statements.
Programming Part 1 Armond R. Smith Zhenying Wu. Overview of this Class ● Transition from FTC -> FRC ● Using Your Resources ● Java Keywords o Data Types.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Access Control Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java 程序设计 Java Programming Fall, Contents for Today Java Program Structure  How to Compile a Java Program  How to Run a Java Program Environment.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Packages F Package is a container for classes F A package is a grouping of related types (classes and interfaces) providing access protection and name.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Java Spring PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.
CSCE 2013L: Lab 1 Overview  Java Basics The JVM Anatomy of a Java Program  Object-Oriented Programming Overview  Example: Payroll.java JDK Tools and.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Introduction to Java Programming with Forte Y. Daniel Liang.
COP 3330 Notes 1/12. Today's topics Downloading Java and Eclipse Hello World Basic control structures Basic I/O Strings.
Introduction to java Prepared By:-Pragnesh Patel Lect. In Computer Dept. NSIT,Jetalpur 1.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
Java programming Package. A package is a group of similar types of classes, interfaces and sub-packages. Package can be categorized in two form, built-
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Lecture 1. Introduction to Programming and Java MIT- AITI 2003.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
1 Features of Java (2) CS 3331 Sections 4.5 and 4.6.
Building Packages BCIS 3680 Enterprise Programming.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
Packages. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L13: Layout Managers Slide 2.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS Lecture 4-1 Java's Work Horses.
3/5/2002e-business and Information Systems1 Java Java Java Virtual Machine (JVM) Java Application Program Interface (API) HW Kernel API Application Programs.
OOP Basics Classes & Methods (c) IDMS/SQL News
ITP 109 Week 2 Trina Gregory Introduction to Java.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Spring 2006 Special Topics in Computer Engineering: Java Intro 1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J.
Java Packages  What are Java packages?  Why do we need packages?  How to create a Java package?  Some examples.  Coming Next: Program Documentation.
The eclipse IDE IDE = “Integrated Development Environment”
Topic: Packages Course : JAVA PROGRAMMING Paper Code: ETCS-307
Packages When we name a program , we name it by class name…
CompSci 230 Software Construction
Java Basics Packages.
Interfaces.
Packages Written part of final exam Alex Rudniy
Packages and Interfaces
Sampath Kumar S Assistant Professor, SECE
PACKAGES.
CMPE212 – Reminders Assignment 2 due today, 7pm.
How to Run a Java Program
Presentation transcript:

Java Package Advantage of Java Package A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be categorized in two form, 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. Advantage of Java Package Java package is used to categorize the classes and interfaces so that they can be easily maintained. 2) Java package provides access protection. 3) Java package removes naming collision.

Many times when we get a chance to work on a small project, one thing we intend to do is to put all java files into one single directory. It is quick, easy and harmless. However if our small project gets bigger, and the number of files is increasing, putting all these files into the same directory would be a nightmare for us. In java we can avoid this sort of problem by using Packages. Packages are nothing more than the way we organize files into different directories according to their functionality, usability as well as category they should belong to. An obvious example of packaging is the JDK package from SUN (java.xxx.yyy) as shown below:

Simple example of java package The package keyword is used to create a package in java. //save as Simple.java   package mypack;   public class Simple{    public static void main(String args[]){       System.out.println("Welcome to package");      }   }   How to compile java package If you are not using any IDE, you need to follow the syntax given below: javac -d directory javafilename    For example javac -d . Simple.java   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).

Output:Welcome to package . To Compile: javac -d . Simple.java To Run: java mypack.Simple Output:Welcome to package The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder

How to access package from another package? import package.*; There are three ways to access the package from outside the package. import package.*; import package.classname; fully qualified name.

Note: Sequence of the program must be package then import then class.

Basically, files in one directory (or package) would have different functionality from those of another directory. For example, files in java.io package do something related to I/O, but files in java.net package give us the way to deal with the Network. In GUI applications, it's quite common for us to see a directory with a name "ui" (user interface), meaning that this directory keeps files related to the presentation part of the application. On the other hand, we would see a directory called "engine", which stores all files related to the core functionality of the application instead.

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 use java.util as a package name for the Vector class (java.util.Vector). So our Vector class can be named as "Vector" or we can put it into another package like com.mycompany.Vector without fighting with anyone. The benefits of using package reflect the ease of maintenance, organization, and increase collaboration among developers. Understanding the concept of package will also help us manage and use files stored in jar files in more efficient ways.

How to create a package Suppose we have a file called HelloWorld.java, and we want to put this file in a package world. First thing we have to do is to specify the keyword package with the name of the package we want to use (world in our case) on top of our source file, before the code that defines the real classes in the package, as shown in our HelloWorld class below: // only comment can be here package world; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } One thing you must do after creating a package for the class is to create nested subdirectories to represent package hierachy of the class. In our case, we have the world package, which requires only one directory. So, we create a directory world and put our HelloWorld.java into it.

That's it. Right now we have HelloWorld class inside world package That's it!!! Right now we have HelloWorld class inside world package. Next, we have to introduce the world package into our CLASSPATH.

Setting up the CLASSPATH From figure 2 we put the package world under C:. So we just set our CLASSPATH as: set CLASSPATH=.;C:\; We set the CLASSPATH to point to 2 places, . (dot) and C:\ directory. Note: If you used to play around with DOS or UNIX, you may be familiar with . (dot) and .. (dot dot). We use . as an alias for the current directory and .. for the parent directory. In our CLASSPATH we include this . for convenient reason. Java will find our class file not only from C: directory but from the current directory as well. Also, we use ; (semicolon) to separate the directory location in case we keep class files in many places.

When compiling HelloWorld class, we just go to the world directory and type the command: C:\world\javac HelloWorld.java If you try to run this HelloWorld using java HelloWorld, you will get the following error: C:\world>java HelloWorld Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: world/HelloWorld) at java.lang.ClassLoader.defineClass0(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:442) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:101) at java.net.URLClassLoader.defineClass(URLClassLoader.java:248) at java.net.URLClassLoader.access$1(URLClassLoader.java:216) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:191) at java.lang.ClassLoader.loadClass(ClassLoader.java:290) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) The reason is right now the HelloWorld class belongs to the package world. If we want to run it, we have to tell JVM about its fully-qualified class name (world.HelloWorld) instead of its plain class name (HelloWorld). C:\world>java world.HelloWorld C:\world>Hello World Note: fully-qualified class name is the name of the java class that includes its package name

To make this example more understandable, let's put the HelloWorld class along with its package (world) be under C:\myclasses directory instead. The new location of our HelloWorld should be as shown in Figure 3:

We just changed the location of the package from C:\world\HelloWorld We just changed the location of the package from C:\world\HelloWorld.java to C:\myclasses\world\HelloWorld.java. Our CLASSPATH then needs to be changed to point to the new location of the package world accordingly. set CLASSPATH=.;C:\myclasses; Thus, Java will look for java classes from the current directory and C:\myclasses directory instead. Someone may ask "Do we have to run the HelloWorld at the directory that we store its class file everytime?". The answer is NO. We can run the HelloWorld from anywhere as long as we still include the package world in the CLASSPATH. For example, C:\>set CLASSPATH=.;C:\; C:\>set CLASSPATH // see what we have in CLSSPATH CLASSPATH=.;C:\; C:\>cd world C:\world>java world.HelloWorld Hello World C:\world>cd .. C:\>java world.HelloWorld

Subpackage (package inside another package) Assume we have another file called HelloMoon.java. We want to store it in a subpackage "moon", which stays inside package world. The HelloMoon class should look something like this: package world.moon; public class HelloMoon { private String holeName = "rabbit hole"; public getHoleName() { return hole; } public setHole(String holeName) { this.holeName = holeName; If we store the package world under C: as before, the HelloMoon.java would be c:\world\moon\HelloMoon.java as shown in Figure 4 below:

Although we add a subpackage under package world, we still don't have to change anything in our CLASSPATH. However, when we want to reference to the HelloMoon class, we have to use world.moon.HelloMoon as its fully-qualified class name.

2) Use an "import" keyword: How to use package There are 2 ways in order to use the public classes stored in package. 1. Declare the fully-qualified class name. For example, ... world.HelloWorld helloWorld = new world.HelloWorld(); world.moon.HelloMoon helloMoon = new world.moon.HelloMoon(); String holeName = helloMoon.getHoleName(); 2) Use an "import" keyword: import world.*; // we can call any public classes inside the world package import world.moon.*; // we can call any public classes inside the world.moon package import java.util.*; // import all public classes from java.util package import java.util.Hashtable; // import only Hashtable class (not all classes in java.util package) Thus, the code that we use to call the HelloWorld and HelloMoon class should be HelloWorld helloWorld = new HelloWorld(); // don't have to explicitly specify world.HelloWorld anymore HelloMoon helloMoon = new HelloMoon(); // don't have to explicitly specify world.moon.HelloMoon anymore Note that we can call public classes stored in the package level we do the import only. We can't use any classes that belong to the subpackage of the package we import. For example, if we import package world, we can use only the HelloWorld class, but not the HelloMoon class.

Using classes stored in jar file Jar files are the place where we put a lot of files to be together. We compress these files and make them as a single bundle. Jar files may also include directories, subdirectories to represent class and package hierachy. Normally, we can see what is inside a jar file by using the command jar -tvf fileName.jar as shown in Figure 5:

We can call this class by From figure 5 we see that there is a class called javax.servlet.http.Cookie. We can call this class by import javax.servlet.http.Cookie; // import only Cookie class or import javax.servlet.http.*; // import the whole javax.servlet.http package But we have to include this package in the CLASSPATH as well. set CLASSPATH=.;D:\JSDK2.0\lib\jsdk.jar; Note that if the package is stored inside a jar file, we have to include the jar file with its extension (.jar) in the CLASSPATH. However, if the package is a plain directory, we just put the name of directory into the CLASSPATH.