my dog learned polymorphism
The moose likes Beginning Java and the fly likes Strange array problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strange array problem" Watch "Strange array problem" New topic

Strange array problem

Parka Teoh
Ranch Hand

Joined: Aug 28, 2004
Posts: 40
I create the simplest array and it doesn't work...

Here's the java file with the array code

It gives me the following error message..

D:\webapp_proj2\WEB-INF\classes\limited\ ']' expected
myDouble[0] = 21.2;
D:\webapp_proj2\WEB-INF\classes\limited\ <identifier> expected
myDouble[0] = 21.2;

I'm lost.

Do you have a parka?<br /> <br /><a href="" target="_blank" rel="nofollow">Visit my homepage</a> | <a href="" target="_blank" rel="nofollow">Parka Deviantart gallery</a>
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Rikki Bostelmann

Joined: Sep 25, 2004
Posts: 8

class test {
int i = 0;


class test {
int i;
i = 0;

The first compiles just fine, whereas in the second example the compiler complains "identifier required". The reason is the following:
classes have state and behavior. The state is given in the instance variables, which may be instantiated at once (1st example), and the behavior is given by methods.
The line i = 0; in the second example would never really be considered: it's not considered when constructing an instance of the class (only int i; till the ; is read), and is not in any method.
So the compiler doesn't know what to do with that line and assumes you forgot the identifier ("int" i = 0; or "char" i = 0; ) .

(Believe me, exactly this mistake took me a while when I first ran into it!)

All the best,
[ October 18, 2004: Message edited by: Rikki Bostelmann ]
Parka Teoh
Ranch Hand

Joined: Aug 28, 2004
Posts: 40
Hey thanks a lot!!!

I have created a constructor to read the values into the array.

Another problem came up, though.

This file is in a package.

When I use another file to reference the array index myDouble[0] and print out using the command prompt, the result was 0.0, instead of the 21.2 I read it into the first file ( in this case)

Here's my other file called

Parka Teoh
Ranch Hand

Joined: Aug 28, 2004
Posts: 40
Oh okay, solved. THanks to all who replied.

I had to instantiate Pack1 as an object inside Pack2.

Man, fundamentals.
I agree. Here's the link:
subject: Strange array problem
It's not a secret anymore!