This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.) sev
Joined: Feb 24, 2004
Thanks for your reply. But I the error is nor from constructor. When I compile this program, it gives BoolArray.java:4: ']' expected a = 1; ^ BoolArray.java:4: <identifier> expected a = 1; ^ BoolArray.java:5: ']' expected b = true; ^ BoolArray.java:5: <identifier> expected b = true; ^
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=1 and b=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.