Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > declare/initialize array in constructor?

Reply
Thread Tools

declare/initialize array in constructor?

 
 
BillJosephson
Guest
Posts: n/a
 
      12-14-2006
Hello. I have a class which will need string arrays. Normally I declare
them as instance variables in the class. But, can these instance
variables be part of the constructor? Here is an example. (1) shows how
I usually declare and instantiate class variables. (2) shows what I
would like to do.

public class chess
{
(1)
int [][] piecesLeft = {{ 1,1,2,2,2,8},{1,1,2,2,2,8}};
String[] pieces = { "King","Queen","Bishop","Knight","Rook","Pawn"
};

// Constructors:
// default
public chess( )
{
(2)
int [][] piecesLeft = {{ 1,1,2,2,2,8},{1,1,2,2,2,8}};
String[] pieces = {"King","Queen","Bishop","Knight","Rook","Pawn"
};
}
}

I tried the code a few different ways, but jGrasp just gives me errors.
I've looked around on line but haven't been able to find this
addressed.

Thanks for any help.

 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      12-14-2006
You can declare the 2 arrays as instance variables, and initialize them
in the constructor. What error message are you getting?

-cheers,
Manish

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      12-14-2006
BillJosephson wrote:
> Hello. I have a class which will need string arrays. Normally I declare
> them as instance variables in the class. But, can these instance
> variables be part of the constructor? Here is an example. (1) shows how
> I usually declare and instantiate class variables. (2) shows what I
> would like to do.
>
> public class chess
> {
> (1)
> int [][] piecesLeft = {{ 1,1,2,2,2,8},{1,1,2,2,2,8}};
> String[] pieces = { "King","Queen","Bishop","Knight","Rook","Pawn"
> };
>
> // Constructors:
> // default
> public chess( )
> {
> (2)
> int [][] piecesLeft = {{ 1,1,2,2,2,8},{1,1,2,2,2,8}};
> String[] pieces = {"King","Queen","Bishop","Knight","Rook","Pawn"
> };
> }
> }
>
> I tried the code a few different ways, but jGrasp just gives me errors.
> I've looked around on line but haven't been able to find this
> addressed.
>
> Thanks for any help.
>


You can certainly put those variable declarations in your constructor
but you can only reference them from the constructor then. You can
declare them as class variables and define them in the constructor.

What is the error?
 
Reply With Quote
 
Ian Wilson
Guest
Posts: n/a
 
      12-14-2006
BillJosephson wrote:
> Hello. I have a class which will need string arrays. Normally I declare
> them as instance variables in the class. But, can these instance
> variables be part of the constructor?


Did you mean this sort of thing? ...

public class chess {
int [][] piecesLeft;
String[] pieces;

public chess( ) {
piecesLeft = {{ 1,1,2,2,2,8},{1,1,2,2,2,8}};
pieces = {"King","Queen","Bishop","Knight","Rook","Pawn" };
}
}

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      12-14-2006
BillJosephson wrote:
> Hello.


...again.

Please refrain from multi-posting in future.

Andrew T.

 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      12-14-2006
Ian Wilson wrote:
> public class chess {
> int [][] piecesLeft;
> String[] pieces;
>
> public chess( ) {
> piecesLeft = {{ 1,1,2,2,2,8},{1,1,2,2,2,8}};

^new int[][]
> pieces = {"King","Queen","Bishop","Knight","Rook","Pawn" };

^new String[]
> }
> }


Unfortunately 'varargs' replaced the proposal for generalised array
literals in Java 1.5. You can only use such syntax if initialising as
part of the declaration. Very sucky.

Tom Hawtin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
How to combine 2 int Array into ONE int Array ? S300 Java 4 08-19-2003 07:04 PM
hashed array in array need the keys... and length Daniel Perl 1 08-14-2003 06:49 PM



Advertisments