For Programmers: Free Programming Magazines  


Home > Archive > Java Help > December 2006 > Can you declare classes' variable in constructor?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Can you declare classes' variable in constructor?
BillJosephson

2006-12-13, 10:09 pm

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
{

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


// Constructors:
// default
public chess( )
{
int [][] piecesLeft = {{ 1,1,2,2,2,8},{1,1,2,2,2,8}};
(2)
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.

Ingo R. Homann

2006-12-14, 9:05 am

Hi,

just like Andrew, I am not sure if I understand your problem correctly.

Is it just that you stumble about the following?:

String[] s={"a","b","c"};

vs

String[] s=new String[]{"a","b","c"};

Ciao,
Ingo

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com