0

I have a class called table and inside it there is a nested class called Cell. I am dividing the table into cells. I want to create an array of the nested class that called Cell, I referred my self to some explanatory tutorials to know how to instantiate an object from a nested/inner class but, non of them offered any example of how to instantiate an array of a nested class. the below posted are my attempts and eclipse highlights cell = mTable.Cell[27]; with red squiggle

Code: Table_Class:

// declaration
private Table mTable;
private Table.Cell []cell;
...
...
...
package com.example.kotschiena02;

public class Table {

    private int table_X1;
    private int table_Y1;
    private int table_X2;
    private int table_Y2;

    public Table(int x1, int y1, int x2, int y2) {
        this.table_X1 = x1;
        this.table_Y1 = y1;
        this.table_X2 = x2;
        this.table_Y2 = y2;
    }

    public int getTableWidth() {
        return (this.table_X2 - this.table_X1);
    }

    public int getTableHeight() {
        return (this.table_Y2 - this.table_Y1);
    }

    public int getTable_X1() {
        return this.table_X1;
    }

    public int getTable_Y1() {
        return this.table_Y1;
    }

    public int getTable_X2() {
        return this.table_X2;
    }

    public int getTable_Y2() {
        return this.table_Y2;
    }

    private class Cell {
        private int cell_ID;
        private int cell_X1;
        private int cell_Y1;
        private int cell_X2;
        private int cell_Y2;
        private boolean occupancyState;

        public Cell (int id, int x1, int y1, int x2, int y2, boolean occupancyState) {
            this.cell_ID = id;
            this.cell_X1 = x1;
            this.cell_Y1 = y1;
            this.cell_X2 = x2;
            this.cell_Y2 = y2;
            this.occupancyState = occupancyState;
        }

        public int getCell_X1() {
            return this.cell_X1;
        }

        public int getCell_Y1() {
            return this.cell_Y1;
        }

        public int getCell_X2() {
            return this.cell_X2;
        }

        public int getCell_Y2() {
            return this.cell_Y2;
        }

        public int getCell_ID() {
            return this.cell_ID;
        }

        public void setOccupancyState(boolean state) {
            this.occupancyState = state;
        }

        public boolean getOccupancyState() {
            return this.occupancyState;
        }
    }
}

Code: SetupTable():

private void setupTable() {
    // TODO Auto-generated method stub
    Log.i(TAG, "@setupTable:");

    mTable = new Table( 10, 
            ((screenHeight/2)-(2*cardHeight)), 
            (screenWidth-10), 
            ((screenHeight/2)+(2*cardHeight)) );

    cell = mTable.Cell[27];
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Firstly two public class cannot be in the same file. then creating an array of object is like any other array. for example you can say mTalbe.Cell[27]= new Cell(*/parameters if needed/*); – nafas Aug 04 '14 at 10:40
  • Why is this tagged with [tag:ios]? – Duncan Jones Aug 04 '14 at 10:41
  • cell should be marked as *static* – Blackbelt Aug 04 '14 at 10:46
  • Global declaration? In java? Are you serious? – fabian Aug 04 '14 at 10:50
  • @nafas after changing the modifier of the class Cell to private, eclipse highlighted the global declaration `private mTable.Cell []cell;` with red sqyuggle – Amrmsmb Aug 04 '14 at 10:50
  • Read this: ["Global variables in Java"](http://stackoverflow.com/questions/4646577/global-variables-in-java) – fabian Aug 04 '14 at 10:55
  • Just in case you didn't get it: **You can't declare global variables in java**, whether you add the static modifier or not. You have to move the declarations **inside the class** and make them static (or don't make them static, if they are associated with the `Table` object). – fabian Aug 04 '14 at 11:44
  • `You cannot have variables outside of a class`. Also, if you wanted to refer to the Cell as `Table.Cell` then you have to mark that class as a `static class`. However, in this case, you just have to make your variables inside the class. – EpicPandaForce Aug 04 '14 at 12:38

1 Answers1

0
public class Table {

//other properties
private List<Cell> cells;

class Cell {
//properties, getters and setters
}

//getters and setters
}

//Setting Up table
public void setupTable() {
// TODO Auto-generated method stub
Log.i(TAG, "@setupTable:");


Table mTable = new Table( 10, 
        ((screenHeight/2)-(2*cardHeight)), 
        (screenWidth-10), 
        ((screenHeight/2)+(2*cardHeight)) );

Table.Cell cell = mTable.getCells().get(27);
}
gvmani
  • 1,580
  • 1
  • 12
  • 20