您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes
Introduction
Content
Apply
Reflect
Extend
previous.gif
 (3087 bytes)
next.gif
 (2959 bytes)

 
 
Content Index
Content Page # 9

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

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg