Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Array of Objects

Thread view: 
Colin Hemmings - 13 Jan 2006 16:40 GMT
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
news - 13 Jan 2006 17:25 GMT
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
[quoted text clipped - 3 lines]
> 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];

Signature

Best regards,
news                            mailto:Daniel.Drozdzewski@xx.yy

Colin Hemmings - 13 Jan 2006 17:38 GMT
It doesnt make any different, its the same thing.

> Hello Colin,
>
[quoted text clipped - 15 lines]
> You should have in above line
> private Side[] Sides =new Side[4];
Steve W. Jackson - 13 Jan 2006 18:05 GMT
> It doesnt make any different, its the same thing.
>
[quoted text clipped - 19 lines]
> > 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 =
Signature

Steve W. Jackson
Montgomery, Alabama

Colin Hemmings - 13 Jan 2006 18:39 GMT
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

>>It doesnt make any different, its the same thing.
>>
[quoted text clipped - 45 lines]
>
> = Steve =
Steve W. Jackson - 13 Jan 2006 20:31 GMT
> 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...

> >>>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 =
Signature

Steve W. Jackson
Montgomery, Alabama

Gordon Beaton - 13 Jan 2006 18:11 GMT
> public class Square
> {
[quoted text clipped - 8 lines]
> 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

Signature

[  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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.