declare/initialize array in constructor?

B

BillJosephson

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.
 
M

Manish Pandit

You can declare the 2 arrays as instance variables, and initialize them
in the constructor. What error message are you getting?

-cheers,
Manish
 
K

Knute Johnson

BillJosephson said:
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?
 
I

Ian Wilson

BillJosephson said:
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" };
}
}
 
T

Thomas Hawtin

Ian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top