Combining
declaration and definition
The following
Java applet produces an error message when compiled:
import java.applet.Applet;
class SalesArray
extends Applet
{
double
monthlySales[12];
// methods
go here
}
When compiled, the
compiler gives the following error message:
This
seems odd at first, since it appears to be a sensible way to tell the Java
compiler to make an array of twelve doubles called monthlySales.
However, it won't work because Java insists on separating declaration and
definition of arrays. The following line is OK:
double monthlySales[];
It can be read as
"monthlySales is an array of doubles". We can follow this with
monthlySales = new double[12];
Meaning
"make monthlySales an array of 12 doubles". These lines could be combined
into one as follows:
double monthlyTotals[] = new double[12];
As you write programs
using arrays, these issues should become more natural to you.
The general form
for combining the declaration and definition of arrays is as follows:
<type>
<identifier> = new <type>[ <dimension> ];
Back
to top
RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex
University. All rights reserved.
webmaster@globalcampus.com.eg
|