File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes I am a real JAVA beginner, please take a look at this code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I am a real JAVA beginner, please take a look at this code." Watch "I am a real JAVA beginner, please take a look at this code." New topic

I am a real JAVA beginner, please take a look at this code.

Sunny Stone

Joined: Feb 24, 2004
Posts: 5
Q: Why can't i assign a value to array a and b? Thanks.
public class BoolArray{
boolean [] b = new boolean[3];
int[] a = new int[3];
a[0] = 1;
b[1] = true;

public static void main(String [] args) {
BoolArray ba = new BoolArray();
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11952

You don't have a constructor for your BoolArray class. so, when you call new BoolArray(), you get the default constructor, which does nothing.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
The only code you're allowed to have outside of methods are declarations and initializations. Check out this code example...

You're trying to initialize your arrays outside of methods, but not on the line in which they were declared.
Incidentally, a syntax note about setting the value of arrays. All of the following code snippets do exactly the same thing:

Also, you might see someone declare an array as "int x[]" but this is highly frowned upon. An array is a type of data, it's not part of the name of the reference, so you should group it with the type part of the declaration and not the variable name part.
Also, Java2 added the ability to create so-called "anonymous arrays", as in the last two examples. It used to be, before Java2, you either initialized an array with the curly brace notation in the third example above, or you set each element one-by-one as in the first two examples. After Java2, you can set every element of an array using the notation present in the latter two examples. Because this syntax is highly useful and consistent coding practice is highly useful, I suggest you always employ it when you set the entire content of an array at once, even during initialization. It adds very little noise to the code and can be easily cut'n'pasted if you choose to move the initialization elsewhere later. (This means you should always prefer example 4 above to example 3 when directly initializing arrays.)
Sunny Stone

Joined: Feb 24, 2004
Posts: 5
Thanks for your reply. But I the error is nor from constructor. When I compile this program, it gives ']' expected
a[0] = 1;
^ <identifier> expected
a[0] = 1;
^ ']' expected
b[1] = true;
^ <identifier> expected
b[1] = true;
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Hello Sunny,
Welcome to the ranch.
I think you are misunderstanding what the previous responses are saying. The reason you are getting the compile errors is because you have assignment statements (namely the a[0]=1 and b[1]=true) placed outside of a method, constructor, or initialization block. You cannot place this code in the place that you have. So there were suggestions of either moving this into a constructor, or making the assignments when instantiating your arrays. Let�s go through these options...
If you used a constructor, as Fred suggested, you would change your code to:

You could also set the values in another method besides the constructor. Since the arrays are not static, you cannot so this in the main method. (If you are not sure what I mean about static versus non-static, then it may be a case that you have not yet covered that in your studies; if you want to read up on it, by far the best treatise on the subject of static vs. non-static variables and classes that I have ever read is in the book Head First Java.) Here�s an example of how assign values in another method:

If you used a combined instantiation and assignment statement, like Sever suggested, you would have the following:

Note, however, that in this case you are initializing all values of your arrays, not just index 0 of array a and index 1 of array b. There is no way to create a three-element array and only set a particular index of the array. You either set none, or all.
Lastly, you could use an initialization block; an initialization block is a block of code, meaning it is contained between braces, that is executed before an object of the class is created. However, it should be pointed out that initialization blocks are not routinely used and are generally applicable to very specific design decisions and implementations. I would strongly recommend that as a beginner you not use them and therefore I have not included an example.
Which method you use (constructor, method, or assignment at creation) depends on some design decisions. I would say for a beginner that is writing a class to just try some things, the best way is to assign the values in a method (the second code block above.)
I hope that helps clarify things better.
I agree. Here's the link:
subject: I am a real JAVA beginner, please take a look at this code.
It's not a secret anymore!