class keyword

Syntax

   v----------'
>>-+----------+-class-Identifier-+-------------------+->
   +-public---+                  '-extends-ClassType-'
   +-abstract-+
   '-final----'

>-+------------------------------+-ClassBody-><
  '-implements-InterfaceTypeList-'

Description
A class declaration specifies a new reference type, which has the class Object as its ultimate ancestor.  The declaration may include class modifiers, which affect how the class can be used.  If the same modifier appears more than once in a class declaration, a compilation error occurs. If the class is declared as public, then it may be accessed by any Java code that can access its containing package; otherwise it may be accessed only from within its containing package.

The Identifier in a class declaration specifies the name of the class. The entire package where the class is declared forms its scope. If a class is declared in a named package with fully qualified name P,  then the class has the fully qualified name P.Identifier. If the class is in an unnamed package, then the class has the fully qualified name Identifier.

If the identifier naming a class appears as the name of any other class type or interface type declared in the same package, a compilation error occurs. If the identifier naming a class is also declared as a type by a single-type-import declaration in the compilation unit containing the class declaration, a compilation error occurs.

The optional extends clause specifies the direct superclass of the class. A class is said to be a direct subclass of the class that it extends. If a class is declared to be a subclass of itself, a compilation error occurs; if circular declarations are detected when classes are loaded at run time a ClassCircularityError exception is thrown. If the extends clause is omitted, the class java.lang.Object is the direct superclass.

The optional implements clause lists the names of all the interfaces that the class implements. Each interface is a direct superinterface of the class. If the same interface is mentioned more than once in a single implements clause, a compilation error occurs.

A ClassBody may contain declarations of members of the class; that is, fields and methods.  A ClassBody may also include static initializers and declarations of constructors for the class.

Examples
In the following example the class Point is declared in a compilation unit with no package statement, so Point is its fully qualified name:

class Point {int x, y;};

In the next example, however, the fully qualified name of the class Point is vista.Point:

package vista;
class Point {int x, y;};

In the next example, the duplicate declaration of the name Point as both a class and an interface in the same package causes the first compilation error. The attempt to declare the name Vector both by a class type declaration and by a single-type-import declaration causes the second compilation error.

package test;
import java.util.Vector;

class Point {
        int x, y;
}

interface Point{               // compile-time error #1
        int getR();
        int getTheta();
}
class Vector { Point[] pts; }  // compile-time error #2

It is not an error for the identifier that names a class also to name a type that otherwise might be imported by a type-import-on-demand declaration in the compilation unit that contains the class declaration. In the following example, the declaration of the class Vector is permitted even though there is also a class java.util.Vector:

package test;
import java.util.*;
class Vector { Point[] pts; }   // not a compile-time error

Within this compilation unit, the simple name Vector refers to the class test.Vector, not to java.util.Vector (which can still be referred to by code within the compilation unit, but only by its fully qualified name).

ngrelr.gif (548 bytes)
Syntax diagrams
Class constructors
abstract keyword
extends keyword
final keyword
implements keyword
interface keyword
package keyword
public keyword
static keyword

Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.