8.6
Constructor Declarations
CLASSES
class Test {
public static void main(String[] args) {
System.out.println(Z.i);
}
}
produces the output:
0
because the variable initializer for
i
uses the class method
peek
to access the
value of the variable
j
before
j
has been initialized by its variable initializer, at
which point it still has its default value ( 4.5.4).
If a
return
statement ( 14.15) appears anywhere within a static initializer,
then a compile time error occurs.
If the keyword
this
( 15.7.2) or the keyword
super
( 15.10, 15.11) appears
anywhere within a static initializer, then a compile time error occurs.
8.6 Constructor Declarations
The constructor of wharves, bridges, piers, bulk heads,
floats, stays against the sea . . .
Walt Whitman,
Song of the Broad Axe
(1856)
A
constructor
is used in the creation of an object that is an instance of a class:
ConstructorDeclaration:
ConstructorModifiers
opt
ConstructorDeclarator
Throws
opt
ConstructorBody
ConstructorDeclarator:
SimpleTypeName
(
FormalParameterList
opt
)
The
SimpleTypeName
in the
ConstructorDeclarator
must be the simple name of
the class that contains the constructor declaration; otherwise a compile time error
occurs. In all other respects, the constructor declaration looks just like a method
declaration that has no result type.
Here is a simple example:
class Point {
int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
}
176
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved