declare/initialize array in constructor?

Discussion in 'Java' started by BillJosephson, Dec 14, 2006.

  1. 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.
     
    BillJosephson, Dec 14, 2006
    #1
    1. Advertising

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

    -cheers,
    Manish
     
    Manish Pandit, Dec 14, 2006
    #2
    1. Advertising

  3. 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?
     
    Knute Johnson, Dec 14, 2006
    #3
  4. BillJosephson

    Ian Wilson Guest

    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" };
    }
    }
     
    Ian Wilson, Dec 14, 2006
    #4
  5. BillJosephson wrote:
    > Hello.


    ...again.

    Please refrain from multi-posting in future.

    Andrew T.
     
    Andrew Thompson, Dec 14, 2006
    #5
  6. 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
     
    Thomas Hawtin, Dec 14, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred Ma
    Replies:
    10
    Views:
    3,852
    Fred Ma
    Jun 4, 2004
  2. David T. Ashley
    Replies:
    6
    Views:
    555
    Tim Rentsch
    Dec 5, 2004
  3. David T. Ashley
    Replies:
    6
    Views:
    398
    Tim Rentsch
    Dec 5, 2004
  4. Replies:
    7
    Views:
    1,421
    Jakob Bieling
    Jul 21, 2005
  5. KOFKS
    Replies:
    5
    Views:
    1,211
Loading...

Share This Page