1.1 Classes, Packages & ImportsHomepage  « Java5 Certification « 1.1 Classes, Packages & Imports

In this lesson we look at all types of class declaration and extension and how to use the package and import statements.

Lets take a look at the points outlined at the Oracle Website for this part of the certification.

  • Section 1: Declarations, Initialization and Scoping

    • Develop code that declares classes (including abstract and all forms of nested classes), interfaces, and enums, and includes the appropriate use of package and import statements (including static imports).

We will defer our look at interfaces and abstract classes to 1.2 Interfaces / Abstract Classes and nested classes to
1.5 Constructors / Nested Classes.

Class Declarationgo to top of page Top

A class is made up of members that form a blueprint for an object and its behaviour. How a class and its members are declared impacts how and who can see what. We will look at variable declarations in 1.3 Variables & Scope and member declarations in 1.4 Overloading & Overriding. In this lesson we will focus on class declarations, the access modifiers we can use with them and the some rules for declaring classes:

Access Modifiers

The table below shows the types of access available in Java for our top-level classes.

Access modifier Description
publicA top-level class may be declared with the public access modifier, and if it is the class is accessible to all other classes everywhere.
no modifier
package-private / (the default)
If a top-level class has no explicit access modifier, which is the default and is also known as package-private, it is accessible only within its own package.
  • The only access modifier that can be used with a top-level class is public.
  • Only one class can be marked as public per source file.
  • If a class within a source file is marked as public then the name of the source file must exactly match the name of the public class (followed by the .java extension).
  • A source file can include multiple non-public classes.
  • If a source file contains no public class then there is no naming constraints on that source file, ie. it doesn't have to match the name of one of the non-public classes.
  • Aside from access modifiers classes can also be marked with the non-access modifiers abstract, final and strictfp. The strictfp is not part of the certification and so we won't go into details. When a class is marked as abstract it has to be subclassed; when a class is marked as final it can't be subclassed, so these two modifers can never be used together.
  • A class can extend a maximum of one other class.


/*
  Some code showing class rules
*/
class A { }                        // OK
private class A { }                // No, can only use public access modifier with top level class
protected class A { }              // No, can only use public access modifier with top level class
public class A { }                 // OK
public class B extends A { }       // OK
public class C extends B, A { }    // No, can only extend one class
abstract class D { }               // OK
final class E { }                  // OK
abstract final class F { }         // No, cannot use both abstract and final

Packagesgo to top of page Top

In Java we can keep libraries of related classes together using a package and this is in essence what the Java language is, a collection of packaged libraries.

  • If a class is part of a package then the package statement must be the first line of code in a source file.

There are two major benefits to the package approach adopted by Java.

  • A package provides the apparatus for us to group related classes together under the same umbrella.
    1. We need a way to uniquely identify classes and Java doesn't allow us to have two top-level classes with the same name, within the same namespace. By using packaging we can partition the namespace to alleviate naming conflicts.
    2. Within any reasonable sized system we will have classes that relate to different aspects of said system, such as the Model View Controller paradigm. Packaging allows us to separate concerns into areas such as a Model subpackage, a View subpackage and so on. This makes the whole development process easier and more manageable.
  • A package is part of the Java mechanism we use to enforce Encapsulation.
    1. By not marking members within a package with an access modifier, we are saying that these members are package-private and can only be accessed from within this package.
    2. By marking members with the protected access modifier, we are saying that these members can only be accessed from within this package or from a subclass outside the package.

The following table shows usage of a single package statement and an example of a multiple hierarchy package statement.

Package Form Example Description
Single package
package pkg;package A;This file is part of package A.
Multiple package hierarchy
package pkg.subPkg1.subPkg2...subPkgN;package A.B.C;This file is part of Package C, which is a subpackage of B, which is a subpackage of A.

Importsgo to top of page Top

Java allows us to import parts of a package, or even the whole package if required for use in our code and we do this using the import keyword.

  • If a class uses the import keyword to import parts or all of a package, then the import keyword must follow the package statement if there is one, or must be the first line of code in a source file if there is no package statement.

The following table shows how to import a single class from a package and all classes from a package using the import statement.

Import Form Example Description
Single class import
import pkg.class;import java.rmi.Remote;Import the Remote class from java.rmi
Multiple class import
import pkg.subPkg1.subPkg2...subPkgN;import java.rmi.*;Import all java.rmi classes

Static Imports

With the introduction of Java5 a new feature became available when using the import statement which is commonly known as static imports. When using the import keyword followed by static we can import all the static members of a class or interface.

The following table shows how to import a single static member from a package and all static members from a package using the import static statement.

Import Static Form Example Description
Single static member import
import static pkg.staticMember;import static java.lang.Math.acos;Import the acos static member from java.lang.Math
Multiple static member import
import static pkg.allStaticMembers;import static java.lang.Math.*;Import all java.lang.Math static members

Related Java5 Tutorials

Objects & Classes - Class Structure and Syntax
Objects & Classes - Enumerations
OO Concepts - Abstract Classes
OO Concepts - Nested Classes
OO Concepts - Interfaces
API Contents - Packages
API Contents - Packages - Imports
API Contents - Packages - Static Imports

<<  Declarations                    1.2 Interfaces / Abstract Classes  >>

go to home page Java5 Tutor Homepage go to home page Top

All the Java5 Certification Declarations, Initialization and Scoping lessons are listed below. Click a link to go to that lesson.

1: Declarations

1.1 Classes, Packages & Imports

Class Declaration
Packages
Imports

1.2 Interfaces / Abstract Classes

1.3 Variables & Scope

1.4 Overriding & Overloading

1.5 Constructors / Nested Classes