 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Colin Hemmings Guest
|
Posted: Fri Jan 13, 2006 4:40 pm Post subject: Array of Objects |
|
|
Hi there,
I was wondering if anyone could help me with I java programming problem
that I am having?
The problem is with two classes called 'Side' and 'Square' I am looking
for 'Square' to consist of an array of 4 'Side' objects. Here is the
code I have tried to get working:
public class Square
{
private Side Sides[] = new Side[4];
Sides[0] = new Side(); //Left
Sides[1] = new Side(); //Right
Sides[2] = new Side(); //Top
Sides[3] = new Side(); //Bottom
}
There are some more lines of code, but the problem is with the
declaration and filling of the array. The compiler keeps saying that its
expecting an ']' after I have declared the array.
The class 'Side' is working fine and I can manage to get the code to
work if I fill the array in a seperate method, but I want the array to
be filled and created whenever I create an object of type 'Square'
I have tried everything I can think of could somone please help me? I am
probably making a stupid mistake but I am just a novice to java so any
help will be greatly appreciated.
Kind Regards
Colin H
|
|
| Back to top |
|
 |
news Guest
|
Posted: Fri Jan 13, 2006 5:25 pm Post subject: Re: Array of Objects |
|
|
Hello Colin,
Friday, January 13, 2006, 4:40:43 PM, you wrote:
| Quote: | Hi there,
I was wondering if anyone could help me with I java programming problem
that I am having?
The problem is with two classes called 'Side' and 'Square' I am looking
for 'Square' to consist of an array of 4 'Side' objects. Here is the
code I have tried to get working:
public class Square
{
private Side Sides[] = new Side[4];
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
You should have in above line
private Side[] Sides =new Side[4];
--
Best regards,
news mailto:Daniel.Drozdzewski (AT) xx (DOT) yy
|
|
| Back to top |
|
 |
Colin Hemmings Guest
|
Posted: Fri Jan 13, 2006 5:42 pm Post subject: Re: Array of Objects |
|
|
It doesnt make any different, its the same thing.
news wrote:
| Quote: | Hello Colin,
Friday, January 13, 2006, 4:40:43 PM, you wrote:
Hi there,
I was wondering if anyone could help me with I java programming problem
that I am having?
The problem is with two classes called 'Side' and 'Square' I am looking
for 'Square' to consist of an array of 4 'Side' objects. Here is the
code I have tried to get working:
public class Square
{
private Side Sides[] = new Side[4];
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You should have in above line
private Side[] Sides =new Side[4];
|
|
|
| Back to top |
|
 |
Steve W. Jackson Guest
|
Posted: Fri Jan 13, 2006 6:05 pm Post subject: Re: Array of Objects |
|
|
In article <8ERxf.43829$zt1.43049 (AT) newsfe5-gui (DOT) ntli.net>,
Colin Hemmings <colin.hemmings1 (AT) ntlworld (DOT) com> wrote:
| Quote: | It doesnt make any different, its the same thing.
news wrote:
Hello Colin,
Friday, January 13, 2006, 4:40:43 PM, you wrote:
Hi there,
I was wondering if anyone could help me with I java programming
problem
that I am having?
The problem is with two classes called 'Side' and 'Square' I am
looking
for 'Square' to consist of an array of 4 'Side' objects. Here is the
code I have tried to get working:
public class Square
{
private Side Sides[] = new Side[4];
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You should have in above line
private Side[] Sides =new Side[4];
|
It goes further than that... What he's describing is stylistic in
nature, but the convention is that it's better to specify it as the
respondent described because it clearly shows that the variable "Sides"
is "an array of Side objects". In addition, by convention, variables
should always begin with lower case letters and class names with upper
case, so that "sides" should be the proper name.
But the actual problem the compiler is reporting (since it doesn't
enforce coding conventions, only language requirements) is that you have
initialization code (as in "Sides[0] = new Side();") outside of any
method, which is not legal.
Two solutions come to mind, since you say you want the array created and
filled when a new Square is created. One is to do the initialization in
the constructor. The second would be to create the array with the
initializations, like this:
private Side[] sides =
new Side[] {new Side(), new Side(), new Side(), new Side()};
This is often not an option for arrays, but a small one like this can
use it. It might still be better, depending on what else goes on in
your code, to do it in a constructor.
= Steve =
--
Steve W. Jackson
Montgomery, Alabama
|
|
| Back to top |
|
 |
Gordon Beaton Guest
|
Posted: Fri Jan 13, 2006 6:11 pm Post subject: Re: Array of Objects |
|
|
On Fri, 13 Jan 2006 16:40:43 GMT, Colin Hemmings wrote:
| Quote: | public class Square
{
private Side Sides[] = new Side[4];
Sides[0] = new Side(); //Left
Sides[1] = new Side(); //Right
Sides[2] = new Side(); //Top
Sides[3] = new Side(); //Bottom
}
There are some more lines of code, but the problem is with the
declaration and filling of the array. The compiler keeps saying that
its expecting an ']' after I have declared the array.
|
Put the 4 assignment statements in a method, a constructor or an
initialization block. As written, the compiler is only expecting
declarations, not program statements, at that point.
/gordon
--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
|
|
| Back to top |
|
 |
Colin Hemmings Guest
|
Posted: Fri Jan 13, 2006 6:39 pm Post subject: Re: Array of Objects |
|
|
Ok, thank you Steve thats great, I went with a constructor in the end.
As for my declaration of the array are you saying its better the way I
have declared it or is the stylistic way better?
Thanks again
Steve W. Jackson wrote:
| Quote: | In article <8ERxf.43829$zt1.43049 (AT) newsfe5-gui (DOT) ntli.net>,
Colin Hemmings <colin.hemmings1 (AT) ntlworld (DOT) com> wrote:
It doesnt make any different, its the same thing.
news wrote:
Hello Colin,
Friday, January 13, 2006, 4:40:43 PM, you wrote:
Hi there,
I was wondering if anyone could help me with I java programming
problem
that I am having?
The problem is with two classes called 'Side' and 'Square' I am
looking
for 'Square' to consist of an array of 4 'Side' objects. Here is the
code I have tried to get working:
public class Square
{
private Side Sides[] = new Side[4];
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You should have in above line
private Side[] Sides =new Side[4];
It goes further than that... What he's describing is stylistic in
nature, but the convention is that it's better to specify it as the
respondent described because it clearly shows that the variable "Sides"
is "an array of Side objects". In addition, by convention, variables
should always begin with lower case letters and class names with upper
case, so that "sides" should be the proper name.
But the actual problem the compiler is reporting (since it doesn't
enforce coding conventions, only language requirements) is that you have
initialization code (as in "Sides[0] = new Side();") outside of any
method, which is not legal.
Two solutions come to mind, since you say you want the array created and
filled when a new Square is created. One is to do the initialization in
the constructor. The second would be to create the array with the
initializations, like this:
private Side[] sides =
new Side[] {new Side(), new Side(), new Side(), new Side()};
This is often not an option for arrays, but a small one like this can
use it. It might still be better, depending on what else goes on in
your code, to do it in a constructor.
= Steve =
|
|
|
| Back to top |
|
 |
Steve W. Jackson Guest
|
Posted: Fri Jan 13, 2006 8:31 pm Post subject: Re: Array of Objects |
|
|
In article <0uSxf.43365$5v1.18593 (AT) newsfe2-win (DOT) ntli.net>,
Colin Hemmings <colin.hemmings1 (AT) ntlworld (DOT) com> wrote:
| Quote: | Ok, thank you Steve thats great, I went with a constructor in the end.
As for my declaration of the array are you saying its better the way I
have declared it or is the stylistic way better?
Thanks again
|
[ snip ]
Here we go with that top-posting thing again...
| Quote: | You should have in above line
private Side[] Sides =new Side[4];
|
This snippet from the other poster's reply indicates what I'm
describing, which is that it's considered best if you declare your
variable "Sides" to be of type "Side[]" -- or "array of Side". The
code included in your original post said "Side Sides[]" which is
syntactically valid (barring the other issues already addressed), just
not recommended style. Sun's Java coding conventions are at
<http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html>. This is
a great resource for coding style.
= Steve =
--
Steve W. Jackson
Montgomery, Alabama
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|