user3769402
user3769402

Reputation: 211

Attempting to increment by 1 every time a class objects is used, but the count stays the same(JAVA)

I have to create a program that creates a scrabble tile. I have no problem creating the letter and value, but I need to have an id number for each tile and that's where the problem is. For my class objects I have:

char tile;
int value; 
private static int ID = 0;

//Default Constructor
public STile() {
    this.tile = '?';
    this.value = 0;
    STile.ID++;
}

//Constructor with parameters. 
public STile(char tile, int value) {
    this.tile = tile;
    this.value = value;
    STile.ID++;
}

And my getter for this is:

public static int getID() {
    return STile.ID;
}

The tiles are created fine but the ID of every tile is 26(the for loop to create them runs 26 times). So every letter should be a different number 1-26. I tried setting a variable equal to STile.ID and that didn't seem to work either, So I'm back to what I have up top.

EDIT:

So I retried setting it to a variable and it worked.

public LTile(){
    this.tile = '?';
    this.value = 0;
    count = LTile.ID;
    LTile.ID += 1;
}
public LTile(char tile, int value){
    this.tile = tile;
    this.value = value;
    count = LTile.ID;
    LTile.ID += 1;

Upvotes: 1

Views: 909

Answers (1)

Josh
Josh

Reputation: 113

Maybe have two ID values, one static and one not, then after incrementing that static ID, set the objects ID equal to the static ID.

Upvotes: 5

Related Questions