7
CSS446 Spring 2014 Nan Wang

CSS446 Spring 2014 Nan Wang. A Java program consists of a collection of classes. Package is a set of related classes. 2

Embed Size (px)

Citation preview

Page 1: CSS446 Spring 2014 Nan Wang.  A Java program consists of a collection of classes.  Package is a set of related classes. 2

CSS446Spring 2014

Nan Wang

Page 2: CSS446 Spring 2014 Nan Wang.  A Java program consists of a collection of classes.  Package is a set of related classes. 2

A Java program consists of a collection of classes.

Package is a set of related classes.

2

Page 3: CSS446 Spring 2014 Nan Wang.  A Java program consists of a collection of classes.  Package is a set of related classes. 2

To put one of your classes in a package, you must place a line◦ package packageName;

as the first instruction in the source file containing the class.

If you did not include any package statement at the top of your source file, its classes are placed in the default package.

3

Page 4: CSS446 Spring 2014 Nan Wang.  A Java program consists of a collection of classes.  Package is a set of related classes. 2

If you want to use a class from a package, you can refer to it by its full name (package name plus class name).

Import java.util.Scanner;◦ Import all classes from a package

Import java.util.*; java.util.Scanner in=new java.util.Scanner(System.in); Scanner in=new Scanner(System.in); No need to import the classes in the java.lang package

containing the most basic Java classes, such as Math and Object.

No need to import other classes in the same package.

4

Page 5: CSS446 Spring 2014 Nan Wang.  A Java program consists of a collection of classes.  Package is a set of related classes. 2

Use a domain name in reverse to construct an unambiguous package name.

5

Page 6: CSS446 Spring 2014 Nan Wang.  A Java program consists of a collection of classes.  Package is a set of related classes. 2

The path of a class file must match its package name.

the source files for classes in the package com.horstmann.bigjava

6

Page 7: CSS446 Spring 2014 Nan Wang.  A Java program consists of a collection of classes.  Package is a set of related classes. 2

java.lang.System.out.println(x); Coding style

◦ If class names always start with an uppercase letter, and variable, method, and package names always start with a lower-case letter

7