CHAPTER 6
Names are used to refer to entities declared in a Java program. A declared entity (§6.1) is a package, class type, interface type, member (field or method) of a reference type, parameter (to a method, constructor, or exception handler), or local variable.
Names in Java programs are either simple, consisting of a single identifier, or qualified, consisting of a sequence of identifiers separated by ".
" tokens (§6.2).
Every name introduced by a declaration has a scope (§6.3), which is the part of the Java program text within which the declared entity can be referred to by a simple name.
Packages and reference types (that is, class types, interface types, and array types) have members (§6.4). A member can be referred to using a qualified name N.
x, where N is a simple or qualified name and x is an identifier. If N names a package, then x is a member of that package, which is either a class or interface type or a subpackage. If N names a reference type or a variable of a reference type, then x names a member of that type, which is either a field or a method.
In determining the meaning of a name (§6.5), Java uses the context of the occurrence to disambiguate among packages, types, variables, and methods with the same name.
Access control (§6.6) can be specified in a class, interface, method, or field declaration to control when access to a member is allowed. Access is a different concept from scope; access specifies the part of the Java program text within which the declared entity can be referred to by a qualified name, a field access expression (§15.10), or a method invocation expression (§15.11) in which the method is not specified by a simple name. The default access is that a member can be accessed anywhere within the package that contains its declaration; other possibilities are public
, protected
, and private
.
Fully qualified names (§6.7) and naming conventions (§6.8) are also discussed in this chapter.
The name of a field, parameter, or local variable may be used as an expression (§15.13.1). The name of a method may appear in an expression only as part of a method invocation expression (§15.11). The name of a class or interface type may appear in an expression only as part of a class instance creation expression (§15.8), an array creation expression (§15.9), a cast expression (§15.15), or an instanceof
expression (§15.19.2), or as part of a qualified name for a field or method. The name of a package may appear in an expression only as part of a qualified name for a class or interface type.
package
declaration (§7.4)
abstract
method of an interface (§9.4)
catch
clause of a try
statement (§14.18)
There are two forms of names: simple names and qualified names. A simple name is a single identifier. A qualified name consists of a name, a ".
" token, and an identifier.
In determining the meaning of a name (§6.5), the Java language takes into account the context in which the name appears. It distinguishes among contexts where a name must denote (refer to) a package (§6.5.3), a type (§6.5.4), a variable or value in an expression (§6.5.5), or a method (§6.5.6).
Not all identifiers in Java programs are a part of a name. Identifiers are also used in the following situations:
.
" token to indicate a member of an object that is the value of an expression or the keyword super
that appears before the ".
" token
.
" token and before a "(
" token to indicate a method to be invoked for an object that is the value of an expression or the keyword super
that appears before the ".
" token
break
(§14.13) and continue
(§14.14) statements that refer to statement labels
class Test { public static void main(String[] args) { Class c = System.out.getClass(); System.out.println(c.toString().length() + args[0].length() + args.length); } }the identifiers
Test
, main
, and the first occurrences of args
and c
are not names;
rather, they are used in declarations to specify the names of the declared entities.
The names String
, Class
, System.out.getClass,
System.out.println
,
c.toString
, args
, and args.length
appear in the example. The first occurrence
of length
is not a name, but rather an identifier appearing in a method invocation
expression (§15.11). The second occurrence of length
is not a name, but
rather an identifier appearing in a method invocation expression (§15.11).
The identifiers used in labeled statements and their associated break
and continue
statements are completely separate from those used in declarations. Thus, the following code is valid:
class TestString {This code was taken from a version of the class
char[] value;
int offset, count;
int indexOf(TestString str, int fromIndex) { char[] v1 = value, v2 = str.value; int max = offset + (count - str.count); int start = offset + ((fromIndex < 0) ? 0 : fromIndex); i: for (int i = start; i <= max; i++)
{ int n = str.count, j = i, k = str.offset; while (n-- != 0) { if (v1[j++] != v2[k++]) continue i; } return i - offset; } return -1; } }
String
and its method indexOf
(§20.12.26), where the label was originally called test
. Changing the label to
have the same name as the local variable i
does not hide the label in the scope of
the declaration of i
. The identifier max
could also have been used as the statement
label; the label would not hide the local variable max
within the labeled statement.
package
declaration, is determined by the host system (§7.4.3). All Java code is within the scope of the standard package named java
, so the package java
can always be referred to by Java code.
class Test { int i = j; // compile-time error: incorrect forward reference int j = 1; }
class Test { Test() { k = 2; } int j = 1; int i = j; int k; }
Test
refers to the field k
that is declared three lines later.
for
statement (§14.12) includes all of the following:
for
statement
for
statement
catch
clause of a try
statement (§14.18) is the entire block associated with the catch
.
package points;
class Point { int x, y; PointList list; Point next; } class PointList { Point first; }the use of
PointList
in class Point
is correct, because the scope of the class type
name PointList
includes both class Point
and class PointList
, as well as any
other type declarations in other compilation units of package points
.
class Test { static int x = 1; public static void main(String[] args) { int x = 0; System.out.print("x=" + x); System.out.println(", Test.x=" + Test.x); } }produces the output:
x=0, Test.x=1This example declares:
Test
static
) variable x
that is a member of the class Test
main
that is a member of the class Test
args
of the main
method
x
of the main
method
x
would normally be available throughout the entire body of the method main
. In this example, however, the class variable x
is hidden within the body of the method main
by the declaration of the local variable x
.
A local variable has as its scope the rest of the block in which it is declared (§14.3.2); in this case this is the rest of the body of the main
method, namely its initializer "0
" and the invocations of print
and println
.
x
" in the invocation of print
refers to (denotes) the value of the local variable x
.
println
uses a qualified name (§6.6) Test.x
, which uses the class type name Test
to access the class variable x
, because the declaration of Test.x
is hidden at this point and cannot be referred to by its simple name.
class Point { int x, y; } class Test { static Point Point(int x, int y) { Point p = new Point(); p.x = x; p.y = y; return p; }This compiles without error and executes to produce the output:
public static void main(String[] args) { int Point; Point[] pa = new Point[2]; for (Point = 0; Point < 2; Point++) { pa[Point] = new Point(); pa[Point].x = Point; pa[Point].y = Point; } System.out.println(pa[0].x + "," + pa[0].y); System.out.println(pa[1].x + "," + pa[1].y); Point p = Point(3, 4); System.out.println(p.x + "," + p.y); }
}
0,0 1,1Within the body of
3,4
main
, the lookups of Point
find different declarations depending
on the context of the use:
new
Point[2]
", the two occurrences of the class instance creation expression "new
Point()
", and at the start of three different local variable declaration statements, the Point
is a TypeName (§6.5.4) and denotes the class type Point
in each case.
Point(3,
4)
" the occurrence of Point
is a MethodName (§6.5.6) and denotes the class (static
) method Point
.
Point
.
import java.util.*;
class Vector { int val[] = { 1 , 2 }; }compiles and prints:
class Test { public static void main(String[] args) { Vector v = new Vector(); System.out.println(v.val[0]); } }
1using the class
Vector
declared here in preference to class java.util.Vector
that might be imported on demand.
This section provides an overview of the members of packages and reference types here, as background for the discussion of qualified names and the determination of the meaning of names. For a complete description of membership, see §7.1, §8.2, §9.2, and §10.7.
In general, the subpackages of a package are determined by the host system (§7.2). However, the standard package java
always includes the subpackages lang
, util
, io
, and net
and may include other subpackages. No two distinct members of the same package may have the same simple name (§7.1), but members of different packages may have the same simple name. For example, it is possible to declare a package:
package vector; public class Vector { Object[] vec; }that has as a member a
public
class named Vector
, even though the standard
package java.util
also declares a class named Vector
. These two class types
are different, reflected by the fact that they have different fully qualified names
(§6.7). The fully qualified name of this example Vector
is vector.Vector
,
whereas java.util.Vector
is the fully qualified name of the standard Vector
class. Because the package vector
contains a class named Vector
, it cannot also
have a subpackage named Vector
.
Object
has no direct superclass)
There is no restriction against a field and a method of a class type having the same simple name.
A class may have two or more fields with the same simple name if they are declared in different interfaces and inherited. An attempt to refer to any of the fields by its simple name results in a compile-time error (§6.5.6.2, §8.2).
interface Colors { int WHITE = 0, BLACK = 1; } interface Separates { int CYAN = 0, MAGENTA = 1, YELLOW = 2, BLACK = 3; } class Test implements Colors, Separates { public static void main(String[] args) { System.out.println(BLACK); // compile-time error: ambiguous } }the name
BLACK
in the method main
is ambiguous, because class Test
has two
members named BLACK
, one inherited from Colors
and one from Separates
.
A class type may have two or more methods with the same simple name if the methods have different signatures (§8.4.2), that is, if they have different numbers of parameters or different parameter types in at least one parameter position. Such a method member name is said to be overloaded.
A class type may contain a declaration for a method with the same name and the same signature as a method that would otherwise be inherited from a superclass or superinterface. In this case, the method of the superclass or superinterface is not inherited. If the method not inherited is abstract
, then the new declaration is said to implement it; if the method not inherited is not abstract
, then the new declaration is said to override it.
class Point { float x, y; void move(int dx, int dy) { x += dx; y += dy; } void move(float dx, float dy) { x += dx; y += dy; } public String toString() { return "("+x+","+y+")"; } }the class
Point
has two members that are methods with the same name, move
.
The overloaded move
method of class Point
chosen for any particular method
invocation is determined at compile time by the overloading resolution procedure
given in §15.11.
In this example, the members of the class Point
are the float
instance variables x
and y
declared in Point
, the two declared move
methods, the declared toString
method, and the members that Point
inherits from its implicit direct superclass Object
(§4.3.2), such as the method hashCode
(§20.1.4). Note that Point
does not inherit the toString
method (§20.1.2) of class Object
because that method is overridden by the declaration of the toString
method in class Point
.
interface Colors { int WHITE = 0, BLACK = 1; } interface Separates { int CYAN = 0, MAGENTA = 1, YELLOW = 2, BLACK = 3; } interface ColorsAndSeparates extends Colors, Separates {the members of the interface
int DEFAULT = BLACK; // compile-time error: ambiguous
}
ColorsAndSeparates
include those members
inherited from Colors
and those inherited from Separates
, namely WHITE
,
BLACK
(first of two), CYAN
, MAGENTA
, YELLOW
, and BLACK
(second of two). The
member name BLACK
is ambiguous in the interface ColorsAndSeparates
.
Object
(§4.3.2, §20.1)
length
, which is a constant (final
) field of every array; its type is int
and it contains the number of components of the array
class Test { public static void main(String[] args) { int[] ia = new int[3]; int[] ib = new int[6]; System.out.println(ia.getClass() == ib.getClass()); System.out.println("ia has length=" + ia.length); } }produces the output:
true ia has length=3This example uses the method
getClass
inherited from class Object
and the
field length
. The result of the comparison of the Class
objects in the second
println
demonstrates that all arrays whose components are of type int
are
instances of the same array type, which is int[]
.
PackageName:Java's use of context helps to minimize name conflicts between entities of different kinds. Such conflicts will be rare if the naming conventions described in §6.8 are followed. Nevertheless, conflicts may arise unintentionally as types developed by different programmers or different organizations evolve. For example, types, methods, and fields may have the same name. Java never has trouble distinguishing between a method and a field with the same name, since the context of a use always tells whether a method or a field is intended.
Identifier
PackageName.
Identifier TypeName:
Identifier
PackageName.
Identifier ExpressionName:
Identifier
AmbiguousName.
Identifier MethodName:
Identifier
AmbiguousName.
Identifier AmbiguousName:
Identifier
AmbiguousName.
Identifier
.
" in a qualified PackageName
.
" in a qualified TypeName
extends
clause in a class declaration (§8.1.3)
implements
clause in a class declaration (§8.1.4)
extends
clause in an interface declaration (§9.1.3)
catch
clause of a try
statement (§14.18)
instanceof
relational operator (§15.19.2)
(
" in a method invocation expression (§15.11)
.
" in a qualified ExpressionName
.
" in a qualified MethodName
.
" in a qualified AmbiguousName
.
", and an Identifier, then the name to the left of the ".
" is first reclassified, for it is itself an AmbiguousName. There is then a choice:
.
" is reclassified as a PackageName, then there is a further choice:
.
" and that package contains a declaration of a type whose name is the same as the Identifier, then this AmbiguousName is reclassified as a TypeName.
.
" is reclassified as a TypeName, then this AmbiguousName is reclassified as an ExpressionName.
.
" is reclassified as an ExpressionName, then this AmbiguousName is reclassified as an ExpressionName.
and then consider this example code in another package:
package ORG.rpgpoet;
import java.util.Random;
interface Music { Random[] wizards = new Random[4]; }
package bazola;
class Gabriel { static int n = ORG.rpgpoet.Music.wizards.length; }First of all, the name
ORG.rpgpoet.Music.wizards.length
is classified as an
ExpressionName because it functions as a PostfixExpression. Therefore, each of
the names:
ORG.rpgpoet.Music.wizards ORG.rpgpoet.Music ORG.rpgpoet ORGis initially classified as an AmbiguousName. These are then reclassified:
ORG
in any other compilation unit of package bazola
, then the simple name ORG
is reclassified as a PackageName.
rpgpoet
in any compilation unit of package ORG
(and we know that there is no such class or interface because package ORG
has a subpackage named rpgpoet
), the qualified name ORG.rpgpoet
is reclassified as a PackageName.
ORG.rpgpoet
has an interface type named Music
, the qualified name ORG.rpgpoet.Music
is reclassified as a TypeName.
ORG.rpgpoet.Music
is a TypeName, the qualified name ORG.rpgpoet.Music.wizards
is reclassified as an ExpressionName.
.
Id, then Q must also be a package name. The
package name Q.
Id names a package that is the member named Id within the
package named by Q. If Q does not name an accessible package or Id does not
name an accessible subpackage of that package, then a compile-time error occurs.
.
Id, then Q must be a package name. The type
name Q.
Id names a type that is the member named Id within the package named
by Q. If Q does not name an accessible package, or Id does not name a type within
that package, or the type named Id within that package is not accessible (§6.6),
then a compile-time error occurs.
package wnj.test;
class Test { public static void main(String[] args) { java.util.Date date = new java.util.Date(System.currentTimeMillis()); System.out.println(date.toLocaleString()); } }produced the following output the first time it was run:
Sun Jan 21 22:56:29 1996In this example:
wnj.test
must name a package on the host system. It is resolved by first looking for the package wnj
, using the procedure described in §6.5.3.1, and then making sure that the subpackage test
of this package is accessible.
java.util.Date
(§21.3) must denote a type, so we first use the procedure recursively to determine if java.util
is an accessible package, which it is, and then look to see if the type Date
is accessible in this package.
final
(§8.3.1.2), then the expression name denotes the value of the field. The type of the expression name is the declared type of the field. If the Identifier appears in a context that requires a variable and not a value, then a compile-time error occurs.
static
method (§8.4.3.2), static initializer (§8.5), or initializer for a static
variable (§8.3.1.1, §12.4.2), then a compile-time error occurs.
class Test {the names used as the left-hand-sides in the assignments to
static int v;
static final int f = 3;
public static void main(String[] args) { int i; i = 1; v = 2; f = 33; // compile-time error System.out.println(i + " " + v + " " + f); }
}
i
, v
, and f
denote the
local variable i
, the field v
, and the value of f
(not the variable f
, because f
is a
final
variable). The example therefore produces an error at compile time
because the last assignment does not have a variable as its left-hand side. If the
erroneous assignment is removed, the modified code can be compiled and it will
produce the output:
1 2 3
.
Id, then Q has already been classified as a
package name, a type name, or an expression name:
static
), then a compile-time error occurs.
final
, then Q.
Id denotes the value of the class variable. The type of the expression Q.
Id is the declared type of the class variable. If Q.
Id appears in a context that requires a variable and not a value, then a compile-time error occurs.
.
Id denotes the class variable. The type of the expression Q.
Id is the declared type of the class variable.
.
Id denotes the value of the field. The type of the expression Q.
Id is the declared type of the field. If Q.
Id appears in a context that requires a variable and not a value, then a compile-time error occurs.
final
field of a class type (which may be either a class variable or an instance variable)
final
field length
of an array type
.
Id denotes the value of the field. The type of the expression Q.
Id is the declared type of the field. If Q.
Id appears in a context that requires a variable and not a value, then a compile-time error occurs.
.
Id denotes a variable, the field Id of class T, which may be either a class variable or an instance variable. The type of the expression Q.
Id is the declared type of the field
class Point { int x, y; static int nPoints; } class Test { public static void main(String[] args) { int i = 0; i.x++; // compile-time error Point p = new Point(); p.nPoints(); // compile-time error } }encounters two compile-time errors, because the
int
variable i
has no members,
and because nPoints
is not a method of class Point
.
.
Id, then Q has already been classified as a
package name, a type name, or an expression name. If Q is a package name, then a
compile-time error occurs. Otherwise, Id is the method name to be used for
method invocation. If Q is a type name, then Id must name at least one static
method of the type Q. If Q is an expression name, then let T be the type of the
expression Q; Id must name at least one method of the type T. See §15.11 for further
discussion of the interpretation of qualified method names in method invocation
expressions.
.
" token appears, preceded by some indication of a package, type, or expression
having a type and followed by an Identifier that names a member of the package
or type. These are collectively known as constructs for qualified access.
Java provides mechanisms for access control, to prevent the users of a package or class from depending on unnecessary details of the implementation of that package or class. Access control applies to qualified access and to the invocation of constructors by class instance creation expressions (§15.8), explicit constructor invocations (§8.6.5), and the method newInstance
of class Class
(§20.3.6).
If access is permitted, then the accessed entity is said to be accessible.
public
, then it may be accessed by any Java code that can access the package in which it is declared. If a class or interface type is not declared public
, then it may be accessed only from within the package in which it is declared.
public
, then access is permitted. All members of interfaces are implicitly public
.
protected
, then access is permitted only when one of the following is true:
protected
member is declared.
protected
member is declared, and the access is correct as described in §6.6.2.
private
, then access is permitted only when it occurs from within the class in which it is declared.
protected
Accessprotected
member or constructor of an object may be accessed from outside
the package in which it is declared only by code that is responsible for the implementation
of that object. Let C be the class in which a protected
member or constructor
is declared and let S be the subclass of C in whose declaration the use of
the protected
member or constructor occurs. Then:
protected
member (field or method), let Id be its name. Consider then the means of qualified access:
super.
Id, then the access is permitted.
.
Id, where Q is a TypeName, then the access is permitted if and only if Q is S or a subclass of S.
.
Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.
.
Id, where E is a Primary expression, or by a method invocation expression E.
Id(
. . .)
, where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.
protected
constructor:
super(
. . .)
, then the access is permitted.
new
T(
. . .)
, then the access is not permitted. (A protected
constructor can be accessed by a class instance creation expression only from within the package in which it is defined.)
newInstance
of class Class
(§20.3.6), then the access is not permitted.
and:
package points;
class PointVec { Point[] vec; }
package points;
public class Point { protected int x, y; public void move(int dx, int dy) { x += dx; y += dy; } public int getX() { return x; } public int getY() { return y; } }which declare two class types in the package
points
:
PointVec
is not public
and not part of the public
interface of the package points
, but rather can be used only by other classes in the package.
Point
is declared public
and is available to other packages. It is part of the public
interface of the package points
.
move
, getX
, and getY
of the class Point
are declared public
and so are available to any Java code that uses an object of type Point
.
x
and y
are declared protected
and are accessible outside the package points
only in subclasses of class Point,
and only when they are fields of objects that are being implemented by the code that is accessing them.
protected
access modifier limits access.
public
and Non-public
Classespublic
modifier, access to the class declaration is limited to
the package in which it is declared (§6.6). In the example:
package points;
public class Point { public int x, y; public void move(int dx, int dy) { x += dx; y += dy; } }two classes are declared in the compilation unit. The class
class PointList { Point next, prev; }
Point
is available outside
the package points
, while the class PointList
is available for access only
within the package. Thus a compilation unit in another package can access
points.Point
, either by using its fully qualified name:
package pointsUser;
class Test { public static void main(String[] args) { points.Point p = new points.Point(); System.out.println(p.x + " " + p.y); } }or by using a single-type-import declaration (§7.5.1) that mentions the fully qualfied name, so that the simple name may be used thereafter:
package pointsUser;
import points.Point;
class Test { public static void main(String[] args) { Point p = new Point(); System.out.println(p.x + " " + p.y); } }However, this compilation unit cannot use or import
points.PointList
, which
is not declared public
and is therefore inaccessible outside package points
.
public
, protected
, or private
are specified, a
class member or constructor is accessible throughout the package that contains the
declaration of the class in which the class member is declared, but the class member
or constructor is not accessible in any other package. If a public
class has a
method or constructor with default access, then this method or constructor is not
accessible to or inherited by a subclass declared outside this package.
package points;
public class Point { public int x, y; void move(int dx, int dy) { x += dx; y += dy; } public void moveAlso(int dx, int dy) { move(dx, dy); } }then a subclass in another package may declare an unrelated
move
method, with
the same signature (§8.4.2) and return type. Because the original move
method is
not accessible from package morepoints
, super
may not be used:
package morepoints;
public class PlusPoint extends points.Point { public void move(int dx, int dy) { super.move(dx, dy); // compile-time error moveAlso(dx, dy); } }Because move of
Point
is not overridden by move
in PlusPoint
, the method
moveAlso
in Point
never calls the method move in PlusPoint
.
Thus if you delete the super.move
call from PlusPoint
and execute the test program:
import points.Point; import morepoints.PlusPoint; class Test { public static void main(String[] args) { PlusPoint pp = new PlusPoint(); pp.move(1, 1); }it terminates normally. If move of
}
Point
were overridden by move
in PlusPoint
,
then this program would recurse infinitely, until a StackoverflowError
occurred.
public
Fields, Methods, and Constructorspublic
class member or constructor is accessible throughout the package
where it is declared and from any other package that has access to the package in
which it is declared (§7.4.4). For example, in the compilation unit:
package points;
public class Point {the
int x, y;
public void move(int dx, int dy) { x += dx; y += dy; moves++; }
public static int moves = 0;
}
public
class Point
has as public
members the move
method and the moves
field. These public
members are accessible to any other package that has access
to package points
. The fields x
and y
are not public
and therefore are accessible
only from within the package points
.
protected
Fields, Methods, and Constructorspoint
package declares:
package points;
public class Point {and the
protected int x, y;
void warp(threePoint.Point3d a) { if (a.z > 0) // compile-time error: cannot access a.z a.delta(this); }
}
threePoint
package declares:
package threePoint;
import points.Point;
public class Point3d extends Point {which defines a class
protected int z;
public void delta(Point p) { p.x += this.x; // compile-time error: cannot access p.x p.y += this.y; // compile-time error: cannot access p.y }
public void delta3d(Point3d q) { q.x += this.x; q.y += this.y; q.z += this.z; }
}
Point3d
. A compile-time error occurs in the method delta
here: it cannot access the protected members x
and y
of its parameter p
, because
while Point3d
(the class in which the references to fields x
and y
occur) is a subclass
of Point
(the class in which x
and y
are declared), it is not involved in the
implementation of a Point
(the type of the parameter p
). The method delta3d
can access the protected members of its parameter q
, because the class Point3d
is
a subclass of Point
and is involved in the implementation of a Point3d
.
The method delta
could try to cast (§5.4, §15.15) its parameter to be a Point3d
, but this cast would fail, causing an exception, if the class of p
at run time were not Point3d
.
A compile-time error also occurs in the method warp: it cannot access the protected member z
of its parameter a
, because while the class Point
(the class in which the reference to field z
occurs) is involved in the implementation of a Point
(the type of the parameter a), it is not a subclass of Point
(the class in which z
is declared).
private
Fields, Methods, and ConstructorsA
private
class member or constructor is accessible only within the class body in
which the member is declared and is not inherited by subclasses. In the example:
class Point {the
Point() { setMasterID(); }
int x, y; private int ID; private static int masterID = 0;
private void setMasterID() { ID = masterID++; }
}
private
members ID,
masterID
, and setMasterID
may be used only
within the body of class Point
. They may not be accessed by qualified names,
field access expressions, or method invocation expressions outside the body of the
declaration of Point
.
See §8.6.8 for an example that uses a private
constructor.
boolean
, char
, byte
, short
, int
, long
, float
, or double
.
.
", followed by the simple (member) name of the subpackage.
.
", followed by the simple name of the class or interface.
[]
".
long
is "long
".
java.lang
is "java.lang
" because it is subpackage lang
of package java
.
Object
, which is defined in the package java.lang
, is "java.lang.Object
".
Enumeration
, which is defined in the package java.util
, is "java.util.Enumeration
".
double
" is "double[]
".
String
" is "java.lang.String[][][][]
".
package points;
class Point { int x, y; } class PointVec { Point[] vec; }the fully qualified name of the type
Point
is "points.Point
"; the fully qualified
name of the type PointVec
is "points.PointVec
"; and the fully qualified name
of the type of the field vec
of class PointVec
is "points.Point[]
".
We recommend these conventions for use in all Java programs. However, these conventions should not be followed slavishly if long-held conventional usage dictates otherwise. So, for example, the sin
and cos
methods of the class java.lang.Math
have mathematically conventional names, even though these method names flout Java convention because they are short and are not verbs.
COM
, EDU
, GOV
, MIL
, NET
, ORG
, or a two-letter ISO country code such as UK
or JP
.
Here are examples of hypothetical unique names that might be formed under this
convention:
COM.JavaSoft.jag.Oak ORG.NPR.pledge.driver UK.ac.city.rugby.gameNames of packages intended only for local use should have a first identifier that begins with a lowercase letter, but that first identifier specifically should not be the identifier
java
; package names that start with the identifier java
are reserved to JavaSoft for naming standard Java packages.When package names occur in expressions:
import
declarations (§7.5) can usually be used to make available the type names declared in that package.
Likewise, names of interface types should be short and descriptive, not overly long, in mixed case with the first letter of each word capitalized. The name may be a descriptive noun or noun phrase, which is appropriate when an interface is used as if it were an abstract superclass, such as interfacesClassLoader
SecurityManager
Thread
Dictionary
BufferedInputStream
java.io.DataInput
and java.io.DataOutput
; or it may be an adjective describing a behavior, as for the interfaces java.lang.Runnable
and java.lang.Cloneable
.Hiding involving class and interface type names is rare. Names of fields, parameters, and local variables normally do not hide type names because they conventionally begin with a lowercase letter whereas type names conventionally begin with an uppercase letter.
get
and set
an attribute that might be thought of as a variable V should be named get
V and set
V. An example is the methods getPriority
(§20.20.22) and setPriority
(§20.20.23) of class java.lang.Thread
.
length
, as in class java.lang.String
(§20.12.11).
boolean
condition V about an object should be named is
V. An example is the method isInterrupted
of class java.lang.Thread
(§20.20.32).
to
F. Examples are the method toString
of class java.lang.Object
(§20.1.2) and the methods toLocaleString
(§21.3.27) and toGMTString
(§21.3.28) of class java.util.Date
.
Method names cannot hide or be hidden by other names (§6.5.6).
final
should be in mixed case with a lowercase first
letter and the first letters of subsequent words capitalized. Note that well-designed
Java classes have very few public
or protected
fields, except for fields that are
constants (final
static
fields) (§6.8.5).
Fields should have names that are nouns, noun phrases, or abbreviations for nouns. Examples of this convention are the fields buf
, pos
, and count
of the class java.io.ByteArrayInputStream
(§22.6) and the field bytesTransferred
of the class java.io.InterruptedIOException
(§22.30.1).
Hiding involving field names is rare.
import
declaration (§7.5) can usually be used to make available the type names declared in that package.
final
variables of class
types may conventionally be, a sequence of one or more words, acronyms, or
abbreviations, all uppercase, with components separated by underscore "_
" characters.
Constant names should be descriptive and not unnecessarily abbreviated.
Conventionally they may be any appropriate part of speech. Examples of names
for constants include MIN_VALUE
, MAX_VALUE
, MIN_RADIX
, and MAX_RADIX
of the
class java.lang.Character
.
A group of constants that represent alternative values of a set, or, less frequently, masking bits in an integer value, are sometimes usefully specified with a common acronym as a name prefix, as in:
interface ProcessStates { int PS_RUNNING = 0; int PS_SUSPENDED = 1; }Hiding involving constant names is rare:
cp
for a variable holding a reference to a ColoredPoint
buf
holding a pointer to a buffer
of some kind
b
for a byte
c
for a char
d
for a double
e
for an Exception
f
for a float
i
, j
, and k
for integers
l
for a long
o
for an Object
s
for a String
v
for an arbitrary value of some type
Contents | Prev | Next | Index
Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)
Copyright © 1996 Sun Microsystems, Inc.
All rights reserved
Please send any comments or corrections to doug.kramer@sun.com