Objects, Distribution, and the Internet. Update on Java. Introduction, fundamentals and basic concepts. Outline

Similar documents
Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

1 Shyam sir JAVA Notes

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

Introduction to Programming Using Java (98-388)

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

15CS45 : OBJECT ORIENTED CONCEPTS

Java Programming Course Overview. Duration: 35 hours. Price: $900

Learning objectives. The Java Environment. Java timeline (cont d) Java timeline. Understand the basic features of Java

Core JAVA Training Syllabus FEE: RS. 8000/-

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Introduction to Java

From C++ to Java. Duke CPS

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Programming Language Concepts: Lecture 2

1 OBJECT-ORIENTED PROGRAMMING 1

Java Overview An introduction to the Java Programming Language

1Z Java SE 5 and 6, Certified Associate Exam Summary Syllabus Questions

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Java Training JAVA. Introduction of Java

[Course Overview] After completing this module you are ready to: Develop Desktop applications, Networking & Multi-threaded programs in java.

Index COPYRIGHTED MATERIAL

Selected Java Topics

SYLLABUS JAVA COURSE DETAILS. DURATION: 60 Hours. With Live Hands-on Sessions J P I N F O T E C H

CS 231 Data Structures and Algorithms, Fall 2016

Course Outline. Introduction to java

Java Primer 1: Types, Classes and Operators

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Chapter 6 Introduction to Defining Classes

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Core Java SYLLABUS COVERAGE SYLLABUS IN DETAILS

Advanced Java Programming

Course information. Petr Hnětynka 2/2 Zk/Z

Pace University. Fundamental Concepts of CS121 1

The Java programming environment. The Java programming environment. Java: A tiny intro. Java features

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

PIC 20A The Basics of Java

Programming. Syntax and Semantics

Programming II (CS300)

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

Name of subject: JAVA PROGRAMMING Subject code: Semester: V ASSIGNMENT 1

Java 8 Programming for OO Experienced Developers

SELF-STUDY. Glossary

OOSD. Introduction to JAVA. Giuseppe Lipari Scuola Superiore Sant Anna Pisa. September 29, 2010

OOSD. Introduction to JAVA. Giuseppe Lipari Scuola Superiore Sant Anna Pisa. September 12, 2011

OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3 Hours Full Marks: 70

B.V. Patel Institute of BMC & IT, UTU 2014

Outline. Introduction to Java. What Is Java? History. Java 2 Platform. Java 2 Platform Standard Edition. Introduction Java 2 Platform

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Advanced Object-Oriented Programming Introduction to OOP and Java

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA

Course information. Petr Hnětynka 2/2 Zk/Z

Program Fundamentals

About this exam review

JAVA: A Primer. By: Amrita Rajagopal

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Brief Summary of Java

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

PESIT Bangalore South Campus

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Java TM Introduction. Renaud Florquin Isabelle Leclercq. FloConsult SPRL.

20 Most Important Java Programming Interview Questions. Powered by

STRUCTURING OF PROGRAM

Chapter 1 Introduction to Java

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

VALLIAMMAI ENGINEERING COLLEGE

Methods (Deitel chapter 6)

Programming overview

Lecture 1: Overview of Java

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Games Course, summer Introduction to Java. Frédéric Haziza

Class, Variable, Constructor, Object, Method Questions

The Java Programming Language

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Seminar report Java Submitted in partial fulfillment of the requirement for the award of degree Of CSE

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Introduction. Overview of the Course on Java. Overview of Part 1 of the Course

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Core Java Syllabus. Overview

Java Notes. J Pickering Department of Computer Science The University of York Heslington York YO10 5DD UK

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Methods (Deitel chapter 6)

Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1

Java Foundations Certified Junior Associate

Special Topics: Programming Languages

Transcription:

Objects, Distribution, and the Internet Update on Java CIMPA INRIA UNESCO School Mérida,Universidad de los Andes (Venezuela) January 7-18, 2002 Richard Grin Richard.Grin@unice.fr University de Nice - Sophia Antipolis (France) Thanks to Michel Buffa with whom I teach java courses at the University of Nice and who wrote the first translation in English of these slides. The courses of the first day is an overview of Java in order to be sure that anyone has the basic knowledge to attend next courses Java : introduction page 3 Java : introduction page 4 Outline Introduction, fundamentals and basic concepts n presentation and structure of the java language n Basic concepts of Object Oriented Programming n inheritance, abstract classes and interfaces n error handling n inner classes Threads Examples of API n java.io(serialization) n JDBC Introduction to Design Patterns Introduction, fundamentals and basic concepts Java : introduction page 5 1

Main characteristics of Java object-oriented uses classes Presentation of the language syntax close to C/C++ has a rich set of packages and some tools: the SDK (Software Developer s Kit) portable, because of the JVM (Java Virtual Machine) a goal: «Write once, run everywhere» Not so easy to achieve! Java : introduction page 7 Java : introduction page 8 Other important characteristics Compilation produces byte code multithreaded robust n strong typing n many checks at runtime n no pointers, only references to existing objects n garbage collection suited to Internet n dynamic class loading through the network, at runtime n facilities for writing distributed applications (sockets, RMI, Corba) n secure (inside the language, API and tools) Byte code is independent of the computer Program source AClass.java Compiler Byte code AClass.class Java : introduction page 9 Java : introduction page 10 The Java Virtual Machine (JVM) Java platform The JVM (Java Virtual Machine) executes byte code; it translates bytecode into native code before executing it Checks many things to ensure that the byte code does not try to do something dangerous => a little slow... (improved by the «Just In Time» compilers and hotspot techniques) But very good portability and security Java Program API JVM Real Machine Java : introduction page 11 Java : introduction page 12 2

3 editions Everything is in classes... J2SE : Java 2 Standard Edition J2EE : Enterprise Edition to write server side applications: servlet, JSP, EJB, J2ME : Micro Edition, light version to write embedded applications for javacard, mobiles, In Java, code is only allowed within a class A source program is made of several files that contain classes definitions Java : introduction page 13 Java : introduction page 14 Dynamic class loading Classpath Dynamic loading of classes during execution; no need to have all the classes in memory before starting execution A class is loaded at runtime by a ClassLoader The classpath contains the local search paths for the classes n from the local computer, n remotely, through the network n from database n Every class loader has its own security policy (cannot load any class) Java : introduction page 15 Java : introduction page 16 Two types of programs Independent application Independent application Applet executed within a web browser. Comes embedded in an HTML page Run with : java Circle The entry point of the executable file is the main method of the Circle class public static void main(string[] args) Java : introduction page 17 Java : introduction page 18 3

Applet Steps for running an applet No main method (not always) An applet is run by the JVM of a Web Browser, when it s area in an HTML page is showed The Browser comes with its own JVM. It runs a Java program that, in turn, will load and run the java applets. It calls some particular methods : init(), start(), stop(), destroy() The browser has its own Class Loader with its own security policy (sandbox) HTTP Client 3. ClientJVM executes the applet 1. Ask for the HTML page which contains the applet 2. Loading Web page and then class of the applet HTTP Server Java : introduction page 19 Java : introduction page 20 Architecture of a java source program Classes and Objects Source of a Java application = n set of «.java» files n resource files: images, sounds, security, url of dbms, messages for internationalization, Each «.java» file contains one or more class (or interfaces) definitions A «.java» file contains at most onepublic class definition (with the file name = name of the public class) Java : introduction page 21 Java : introduction page 22 An application is made of objects Application functionalities offered by dynamic collaboration between objects Objects of same type modeled by a class Java : introduction page 23 Interactions between objects Objects interact by sending messages Methods of the class of an object «synchronous messages you can send to it: when the object receives a message, it executes the corresponding method Examples : object1.describeyourself(); employee.setsalary(20000); car.start(); car.setspeed(50); Receiver Sent message Java : introduction page 24 4

Class A class is a template to create objects, called instances of the class A class is made of : n variables (or fields, or attributes): give the instance state n constructors: create the instances n methods: define the types of the messages you can send to the object Variables and methods are called the members of the class Java : introduction page 25 Example : the Book class public class Book { private String title, author; Variables private int nbpages; // Constructor public Book(String atitle, String anauthor) { title = atitle; author = anauthor; public String getauthor() { return author; Constructors // an accessor public void setnbpages(int n) { // a modifier nbpages = nb; Methods Java : introduction page 26 Classes and instances An instance of a class is created by a constructor of the class Once created, the instance n has its own internal state (variables values) n shares the code that determines its behavior (methods) with other instances of the same class Constructors Java : introduction page 27 Java : introduction page 28 Constructors Each class has one or more constructors. A constructor n creates the instances n sets their initial state (initialize variables) A constructor has n the same name as the class n no return type n no return instruction Java : introduction page 29 Creation of an instance public class Employee { private String lastname, firstname; private double salary; public Employee(String ln, String fn) { lastname = ln; firstname = fn; public static void main(string[] args) { Employee e1 = new Employee("Dupond", "Pierre"); e1.setsalary(12000);... Java : introduction page 30 5

Several constructors (overloading) public class Employee { private String lastname, firstname; private double salary; // 2 Constructors public Employee(String ln, String fn) { lastname = ln; firstname = fn; public Employee(String ln, String fn, double s) { lastname = ln; firstname = fn; salary = s;... e1 = new Employee("Drake", "Peter"); e2 = new Employee("Smith", "John", 15000); Java : introduction page 31 Call another constructor with this() public class Employee { private String name, firstname; private double salary; // This constructor calls the other one public Employee(String n, String fn) { this(ln, fn, 0); public Employee(String ln, String fn, double s) { lastname = ln; firstname = fn; salary = s;... e1 = new Employee("Drake", "Peter"); e2 = new Employee("Smith", "John", 15000); Java : introduction page 32 The Default Constructor If a class has no constructor, Java adds automatically a constructor The code of this default constructor is [public] Classe() { Methods Same accessibility as the class (public or not) Java : introduction page 33 Java : introduction page 34 Types of methods Accessors: they reads the state of the object (value of a variable) Modifiers: they modifie the state Other methods that offer more complex services Private methods are accessible only from their class; they are utilities for the other methods Java : introduction page 35 Instance method example public class Employee { private double salary; Modifier... public void setsalary(double asalary) { if (asalary >= 0.0) salary = asalary; public double getsalary() { return salary;... Accessor Java : introduction page 36 6

Overloading In Java, you can overload a method: add a method with the same name but not the same signature as another method: double computesalary(int index) double computesalary(int index, double bonus) It is forbidden to overload a method only by changing the return type: int computesalary(int index) Variables Java : introduction page 37 Java : introduction page 38 Types of variables Initialization of a Variable Instance variables: n declared outside any method n accessible and shared by all the methods Local variables: n declared inside a method n accessible only in the internal block where they are declared, from their declaration to the end of the block A variable must be initialized before being used Instances variable are automatically initialized if you don t initialize them Java : introduction page 39 Java : introduction page 40 Declaration / creation public static void main(string[] args) { Employee e1; e1.setsalary(12000); throws a NullPointerException «Employee e1;» n declare the e1 variable that will be a reference to an object of the Employee class n but no object has been created! We shouldhave written: Employee e1; e1 = new Employee("Drake", "John"); e1.setsalary(12000); Java : introduction page 41 Naming instance members In the code of another class (but variables are often private): circle1.radius Send a message to an instance: circle1.draw() Java : introduction page 42 7

this In an instance method you use this to designate the instance that received the message, the current instance this can be implicit when you work with members of the current instance Implicit this public class Employee {... public void setsalary(double asalary) { salary = asalary; public double getsalary() { return salary;... Implicitly this.salary Implicitly this.salary Java : introduction page 43 Java : introduction page 44 Explicit this this is mainly used: n to distinguish between instance variables and parameter withthe same name: public void setsalary(double salary) this.salary = salary; Class members (static) n when an object wants to pass a reference of itself to an another object: salary =accountant.computesalary(this); Accountant, «compute my salary» Java : introduction page 45 Java : introduction page 46 Class variables Some variables may be shared by all the instances of a class. They are called class variables (static modifier in Java) Unshared variables (whose value differs from one instance to another) are called instance variables Example of a static variable public class Employee { private String name, firstname; private double salary; private static int nbemployees = 0; // Constructor public Employee(String n, String p) { name = n; firstname = p; nbemployees++; Java : introduction page 47 Java : introduction page 48 8

Class methods Work with a class method A class method (static modifier in Java) performs an action not related to a particular instance; it corresponds to a message sent to the class Exemple : static int nbemployees() { return nbemployees; To manipulate a static method from another class code, prefix its name by the name of the class it belongs to: int n = Employee.nbEmployees(); Java : introduction page 49 Java : introduction page 50 Static initialization blocks static blocks can be used to perform complex variable initializations of static variables : class aclass { static int[] anarray = new int[25]; static { for (int i = 0; i < 25; i++) {... // initialization of anarray // end of the static block... static block is executed when the class is loaded in memory Java : introduction page 51 Member accessibility Java : introduction page 52 Access to members private : only the class where the member is defined can use it public : all classes can use it Otherwise, by default, only the classes belonging to the same package as the member class can use it protected : subclasses may manipulate the inherited protected member (studied later) Encapsulation Protect as much as you can the object state (instance variables). Use private! If an instance variable is readable, provide a public accessor, a method that return the value of the variable If you want to allow the modification of an instance variable, provide a public modifier, a method that will modify the variable, verifying that the modification is valid Java : introduction page 53 Java : introduction page 54 9

The granularity of encapsulation is the class UML (Unified Modeling Language) In Java, instance variable protection is class by class, not object by object An object can access all the attributes of another object of the same class, even private attributes Circle privatepoint center private int radius public Circle(Point, int) public void setrdius(int) public int getradius() public double surface() Circle - Point center - int radius + Circle(Point, int) + void setradius(int) + int getradius() + double surface() (- : private,#:protected, + : public, $ : static) Java : introduction page 55 Java : introduction page 56 Data types in Java In java you manipulate 2 kinds of data types: n primitive types n objects (instances) Data types in Java Java manipulates differently these 2 types; variables contain n primitive values n references to objects Java : introduction page 57 Java : introduction page 58 boolean (true/false) Primitive types integers : byte (1 byte), short (2 bytes), int (4 bytes), long (8 bytes) floating point numbers : float (4 bytes), double (8 bytes). character (only one) : char (2 bytes) Unicode encoded (not ASCII) Named constants The final modifier keyword indicates that the value of a variable cannot be modified. Exemple : double static final PI = 3.14; Java : introduction page 59 Java : introduction page 60 10

int m() { A a1, a2; Work with references a1 = new A(); a1 = a2;... What happens when m() is called? int m() { A a1 = new A(); A a2 = a1;... a1 a2 Références Java : introduction page 61 Stack Heap Java : introduction page 62 int m() { A a1 = new A(); A a2 = a1;... Références int m() { A a1 = new A(); A a2 = a1;... Références a1 a2 Instance of A a1 a2 Instance of A Stack Heap Stack Heap Java : introduction page 63 Java : introduction page 64 int m() { A a1 = new A(); A a2 = a1;... Références After the execution of the method m, the instance of A is no longer referenced but it stays in the heap a1 a2 Instance of A Instance of A Stack Heap Stack Heap Java : introduction page 65 Introduction Java : introduction à Java page 66 11

... the garbage collector will make its job later... Garbage collector The garbage collector is a task that n works in background Instance de A n frees the memory from unreferenced instances n compacts free blocks Runs n when the system needs memory n randomly with a low priority n called explicitly by some programmer code Stack Heap Java : introduction page 67 Java : introduction page 68 final variable Cast final variable: n If the variable has a primitive type, its value cannot be changed n If the variable references an object, it will not be able to reference another object, but the referenced object can be changed final Employee e = new Employee("John"); e.name = "Smith"; e.setsalary(12000); e = e2; // Not! In some case you may want to manipulate an expression with a type different from its declared type This is called a cast : (enforced type) expression int x = 10, y = 3; double z = (double) x/ y; Java : introduction page 69 Java : introduction page 70 Authorized Casts In Java, only 2 kind of casts are authorized: n between primitive types, n between superclasses and subclasses (inheritance) Lexical structures Java : introduction page 71 Java : introduction page 72 12

Unicode For identifiers, comments, chars or Strings, Java uses Unicode version 2.0 characters (encoded on 2 bytes) ASCII characters are the 128 first characters of Unicode In a source, the Unicode char whose code is xxxx (hexa) may be written \uxxxx Java name conventions Class names start with a capital letter (they are the only ones): Circle, Object Words contained within an identifier name are Capitalized: AClasse, amethod, avariable, amethodwithalongname Constants are in uppercase with a «_» between words: A_CONSTANT Java : introduction page 73 Java : introduction page 74 Comments One line : // Here is a comment int bonus = 1500; // end of month bonus Several lines : /* first line of comment second line */ javadoc style /** * This method computes... * @return... */ Arrays and basic classes Java : introduction page 75 Java : introduction page 76 Arrays In Java Arrays are Objects (inherits from Object): n array variables reference instances n arrays are created by the new operator n They have an instance variable : final int length However Java has a particular syntax for: n array declaration n array initialization Object arrays Common mistake : initialize an array and use the objects from the array before they have been created Employee[] tabemp = new Employee[100]; tabemp[0].setname("dupond"); Employee[] tabemp = new Employee[100]; tabemp[0] = new Employee(); tabemp[0].setname("dupond"); Creation of the 1st employee Java : introduction page 77 Java : introduction page 78 13

Strings 2 classes : String for constant strings and StringBuffer for variable strings. Use String except for the strings that will be modified often Shortcut to initialize String objects : string = "Hello"; // instead of new String("Hello"); String concatenation String s = "Hello" + " my friends"; If one of the two operands is a String, then the other operand will be converted to String (by the tostring() method if it is an object): int x = 5; s = "Valeur de x = " + x; Java : introduction page 79 Java : introduction page 80 Wrapper classes for primitive types In certain cases you will need to manipulate objects (instances) and not primitivetypes For example, an ArrayList (extensible array ) can only contain objects The java.lang package provides wrapper classes in order to encapsulate primitive types : Byte, Short, Integer, Long, Float, Double, Boolean, Character Control flows Java : introduction page 81 Java : introduction page 82 Hide a variable in a block? Java does not allow the declaration of an identifier in a block if the including block contains an identifier with the same name int sum(int init) { int i = init; int j = 0; for (int i = 0; i < 10; i++) { j += i; loop instructions break exits from the loop and continues after the loop continue stops the iteration in progress and begins the next iteration Java : introduction page 83 Java : introduction page 84 14

Example of continue and break int sum = 0; for (int i = 0; i < tab.length; i++) { if (tab[i] == 0) break; if (tab[i] < 0) continue; sum += tab[i]; System.out.println(tab[i]); System.out.println(sum); Writing methods Java : introduction page 85 Java : introduction page 86 Method header [accessibility] [static] ret -type name([param-list]) public protected private void int Circle... public double salary() int static nbemployees() public void setsalary(double asalary) private int computebonus(int bonustype, double salary) public int m(int i, int j, int k) Java : introduction page 87 Passing parameters Always by value But, for objects, the reference is passed by value So, n if the method modifies the referenced object, the object will be modified even outside of the method n if the method changes the reference, no effects outside Java : introduction page 88 Example of parameter passing public void m(int i, Employee e1, Employee e2) { i = 100; e1.salary = 100; e2 = new Employee("Pierre", 100); public static void main(string[] args) { Employee e1 = new Employee("Patrick", 10000); Employee e2 = new Employee("Bernard", 10000); int i = 10; m(i, e1, e2); System.out.println(i + '\n' + e1.salary + '\n' + e2.salary); Prints 10 100.0 10000.0 final parameters The keywordfinal indicates that a parameter is not modifiable in the method. If primitive type, the value can t be modified : int m(final int x) Otherwise, the reference of the object can t be modified but the object can be modified: int m(final Employe e1) Java : introduction page 89 Java : introduction page 90 15

Return value of a method return expression return i * j; return new Circle(p, x+y); Packages Java : introduction page 91 Java : introduction page 92 Packages: definition Main packages in the SDK Java classes are grouped into packages They correspond to other languages libraries, like C, Fortran, Ada, etc... Packages offer an extra modularity level n group classes depending on their functionalities n protect access to class members The JDK comes with a lot of packages java.lang: basic classes (automatically included) java.util: utilities java.io: inputs and outputs java.awt: GUI java.applet: applets java.net: network java.rmi: distributed objects javax.swing: GUI Java : introduction page 93 Java : introduction page 94 Import classes from a package You can n import a class from the package: import java.util.arraylist; n import all the classes of a package : import java.util.*; Then you can refer to the class by its name: ArrayList list = new ArrayList(); Else you have to use the qualified name of the class: java.util.arraylist list = java.util.arraylist(); Make your own packages package package-name; Must be the first instruction in the «.java» file By default, the «.java» file belongs to the default package. This default package is anonymous and contains all the classes in the same directory Java : introduction page 95 Java : introduction page 96 16

Subpackages A package can contain sub packages For example, the java.awt package contains the java.awt.event package Importation of a package does not import sub packages; you need to write : import java.awt.*; import java.awt.event.*; Name and location of packages Usage is to prefix the name of the package by your email address, fr.unice.grin.liste The.class file location in the file system must correspond to the name of the package: fr/unice/grin/liste/foo.class And the fr directory must be included in one of the directories indicated by the CLASSPATH variable Java : introduction page 97 Java : introduction page 98 Encapsulation of a class in a package If the class definition starts with public class the class is available from anywhere. If the keyword class is not preceded by public, the class can only be accessed from the classes of the same package Executing a class of a package Launch execution of the main method of a class by giving its qualified name For example, if the class C belongs to the p1.p2 package: java p1.p2.c Java : introduction page 99 Java : introduction page 100 17