Download presentation
Presentation is loading. Please wait.
1
Joel Adams and Jeremy Frens Calvin College
Packages Joel Adams and Jeremy Frens Calvin College
2
Organization of Classes
A class encapsulates the data and behavior of an object. Related classes are grouped together in a package. Organization! java.lang for basic Java-language classes (e.g., String, Thread, etc.) java.util for utility classes (e.g., List, Map, StringTokenizer, etc.) java.net for networking classes (e.g., Socket, URL, etc.) javax.swing for the Swing GUI library Etc…
3
Create Your Own Creating a package is implicit:
package com.siemens.foobar; public class Person { … } Must be first statement in file. Creates (or adds to) a package named com.siemens.foobar. Creates a new class with the fully qualified name com.siemens.foobar.Person.
4
Conventions Use lowercase letters (convention).
Separate words (valid Java identifiers) with periods (required). Package name begins with domain name in reverse order (e.g., edu.siemens, com.siemens, edu.calvin, etc.) Rest of package name is your choice.
5
Importing You should import classes you use directly. Wildcard import:
import com.siemens.foobar.*; Targeted import: import com.siemens.foobar.Person; Must come after package declaration and before class definitions. You can skip an import and always use a fully qualified name.
6
Packages and Directories
Package name has impact on directory structure: package com.siemens.foobar; byte-code files project build com siemens foobar source files src com siemens foobar The build and src directories can be the same. A good IDE (e.g., Eclipse, JBuilder) will manage the directories.
7
Visibility Packages add a new wrinkle to the visibilities:
public visibility: accessible anywhere. private visibility: accessible only by the same class. protected visibility: accessible by the same class, by its subclasses, by any other class in the same package. default visibility: accessible within the package.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.