rakeshisu
rakeshisu

Reputation: 179

Using a static variable to create a unique code with each instance of the object

I am trying to create a unique property code every time I create new instance of the House class. For example, when I create the third house in my program, I need it to assign the integer '3' to it so that I can reference the house with that unique code. I have attempted this with a global static variable and while retaining the correct amount of objects created, it only ever returns the last instance's value.

private static int houseNo = 0;
private int propertyCode;

public House(String s, Town t, Person o){
    owner = o;
    street = s;
    town = t;
    houseNo++;
    propertyCode = houseNo;
}
public String toString(){
    String temp = "";
    temp = "Code: " + this.getPropCode() + " \nAddress:\n" + this.getStreet() + ", " + town.getTownName();
    return temp;
}

Say I have created 6 houses in my main class, using the toString to access any house will only return 6 in place of the getPropCode().

Any ideas?

Edit(More code):

public int getPropCode(){
    return propertyCode;
}

And from my main class:

public static void main(String[] args) {
   House house1 = new House("blueberry", town1, fred);
   House house2 = new House("blackberry", town2, barney);
   House house3 = new House("redberry", town3, fred);
   int whichHouse = Integer.parseInt(JOptionPane.showInputDialog("Select a house to create a lease for \n1. " + house1.toString() + "\n2. " + house2.toString() + "\n3. " + house3.toString()));

Please forgive my naming convention, just trying to mess around with this code.

Upvotes: 0

Views: 5269

Answers (3)

upog
upog

Reputation: 5521

are you expecting like this, updated attribute for simplicity

public class Test {     
    private static int count = 0;

    public static void main( String [] args) { 
        House h1 = new House("blueberry", "town1", "fred");
        System.out.println(h1.toString());
        House h2 = new House("blackberry", "town2", "barney");
        System.out.println(h2.toString());
        House h3 =new House("redberry", "town3", "john");;
        System.out.println(h3.toString());              
    } 
}

class House {
    String name;
    String person;
    String town;
    private int propertyCode;
    static int count = 0;

    public House(String name,String town,String person){
        count = count +1;
        this.propertyCode = count;
        this.town = town;
        this.person = person;
        this.name = name;

    }
    public String toString(){
        String temp = "";
        temp = "Code: " + this.name + " " + this.propertyCode;
        return temp;
    }

}

Upvotes: 1

thewaterwalker
thewaterwalker

Reputation: 324

Your code is not thread safe, so you should consider using AtomicInteger

private static AtomicInteger houseNo = new AtomicInteger(0);

public House(String s, Town t, Person o){
    owner  = o;
    street = s;
    town   = t;
    propertyCode = houseNo.incrementAndGet();
}

Specifically, it's these two lines where multiple threads running through the constructor could give you unexpected results

...
houseNo++;
propertyCode = houseNo;
...

Of course, AtomicInteger will only supply unique propertyCodes within a single JVM, but maybe that's all you need.

Upvotes: 1

cark
cark

Reputation: 254

This works fine, so check again your code, and specially your "getPropCode"

public class House{

    private static int houseNo = 0;
    private int propertyCode;

    public House(){
        houseNo++;
        propertyCode = houseNo;
    }

    public int getPropCode(){
        return propertyCode;
    }

    public String toString(){
        return "code: "+getPropCode();
    }

}

public class Test {

    public static void main(String[] args){

        House a = new House();
        House b = new House();
        House c = new House();

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

    }

}

Upvotes: 0

Related Questions