my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Simple Array Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Array Question" Watch "Simple Array Question" New topic
Author

Simple Array Question

Frankie Chee
Ranch Hand

Joined: Oct 26, 2003
Posts: 43
Hi,
when I compile the following code I receive an error saying <identifier> expected and cannot resolve symbol.
What could be the problem?
Below is the code:
__________________________________________________________________________

class array1
{
int bok[];
bok[]=new int[12]
bok[0]=1;
}
__________________________________________________________________________
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
You're missing a semicolon at the end of one of the lines.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Frankie Chee
Ranch Hand

Joined: Oct 26, 2003
Posts: 43
Hi Sheriff,
Glad to see you around
I put in the semi-colon but it prompt me the following error:
______________________________________________________________________
D:\practice\array1.java:7: <identifier> expected
bok[]=new int[12];
^
D:\practice\array1.java:9: ']' expected
bok[0]=1;
^
D:\practice\array1.java:9: <identifier> expected
bok[0]=1;
^
D:\practice\array1.java:7: cannot resolve symbol
symbol : class bok
location: class array1
bok[]=new int[12];
^
D:\practice\array1.java:9: cannot resolve symbol
symbol : class bok
location: class array1
bok[0]=1;
^
5 errors
Tool completed with exit code 1
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
You can declare and instantiate in one line outside of a method, so

but you can not instantiate on a separate line like you are trying

The compiler was looking for an identifier to start the second line and that's responsible for one of the compilation errors.

You can have a block of code which can be used to set up variables:

Note the braces. This code is automatically executed before any main method.
Writing the line without the braces makes the compiler look for a varaible declaration, which is why when it sees the [ after 'bok' it thinks you're declaring an array and looks for the matching ] but the 0 inbetween leads to the " ']' expected " error pointing at 0.
Frankie Chee
Ranch Hand

Joined: Oct 26, 2003
Posts: 43
Hi Carlos,
thanks for your explanation. Very glad you are teaching me things that are not found in most textbooks.(I feel so privilege )
I understand what you mean that after '[', JVM is looking for another '[' because its thinking I am trying in declare int bok[], right?
What does enclosing 'bok[0]=1' with curly braces mean to the JVM? I don't quite understand how executing it before any main method will eradicate the errors which occur previous. Could you explain just a bit more?
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Carlos lost me on that last comment...I see no need for the braces.
Here are all the legal ways (I think I got 'em all, anyway) to declare an array:

sev
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
*snip*
[ March 01, 2004: Message edited by: Carlos Failde ]
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
Originally posted by Frankie Chee:

I understand what you mean that after '[', JVM is looking for another '[' because its thinking I am trying in declare int bok[], right?

Correct.

What does enclosing 'bok[0]=1' with curly braces mean to the JVM? I don't quite understand how executing it before any main method will eradicate the errors which occur previous. Could you explain just a bit more?

There was nothing wrong with the line, it's just that the compiler objected to where you put it. If you had put it inside a method or a constructor, then there would have been no compile errors. But putting it on a line by itself is disallowed.
Blocks of code surrounded by {} are executed when a class is first accessed, before any methods of that class are. How about giving bok[0] a random value?:


Originally posted by sever oon:

Carlos lost me on that last comment...I see no need for the braces.
Here are all the legal ways (I think I got 'em all, anyway) to declare an array


Nice example, I think I'll keep it.
I note that all of those ways where inside a method. What Frankie was attempting was outside of a method which is why I suggested the braces.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Array Question
 
Similar Threads
The method showMessageDialog(Component, Object) in the type JOptionPane is not applic
Literature Question
java code problem
Book Of Knowledge(BOK)
going from textPad to Eclipse