Paul
Paul

Reputation: 1176

Why do my newly constructed objects seem to be identical?

I am currently writing a java program, and I am getting a result that seems to be very strange. I am constructing object of the class SimulatedExchange, which has the following constructor:

public class SimulatedExchange {
public static String exchangeName;
SimulatorData sim;

public static double Fee;
public static Limit buyLimit;
public static Limit sellLimit;

public SimulatedExchange(String exchangename, SimulatorData simdata){
    exchangeName = exchangename;
    sim = simdata;
    if (exchangename.contentEquals("Exchange1")) Fee = .005;
    if (exchangename.contentEquals("Exchange2")) Fee = .0055;
    if (exchangename.contentEquals("Exchange3")) Fee = .002;
    buyLimit = new Limit();
    sellLimit=new Limit();

}

My main class looks like this:

     public static void main (String[] args){
            SimulatorData fake = new SimulatorData("c:/users/el nico/documents/SimulatorData.xls");
    SimulatedExchange Exchange1 = new SimulatedExchange("Exchange1", fake);
      SimulatedExchange Exchange2 = new SimulatedExchange("Exchange2", fake);
    SimulatedExchange Exchange3 = new SimulatedExchange("Exchange3", fake);

       System.out.println("1st exchange name: " + Exchange1.exchangeName);
       System.out.println("2nd exchange name: " + Exchange2.exchangeName);
       System.out.println("3rd exchange name: " + Exchange3.exchangeName);

   }

Where the SimulatorData object contains an ArrayList of data it reads from a file upon construction.

I am getting the following output:

1st exchange name: Exchange3

2nd exchange name: Exchange3

3rd exchange name: Exchange3

This is strange to me, because I have input different strings into the three different constructors for the SimulatedExchange objects names Exchange1, Exchange2, and Exchange3, so I would have thought the constructors would set each of their exchangeName attributes differently. I am somewhat new to java, and I am not sure why this might not be happening. I don't think I am setting the addresses for the different Exchanges to be pointing to the same object in the heap, because I am constructing a new object each time.

One thing I notices is that the objects have the correct exchangeName attributes (and all other attributes) just after they have been constructed, so this means that it is being changed only when another object of the same class is constructed. This suggests to me that Exchange1, Exchange2, and Exchange3 are all pointing to the same object in the heap. But I don't get why this would be happening.

Any insight would be greatly appreciated! I apologize in advance if the answer is very obvious, which it seems it must be. I just can't seem to pinpoint the problem. Thanks, Paul

Upvotes: 1

Views: 67

Answers (3)

musical_coder
musical_coder

Reputation: 3896

You have exchangeName set as static. That means that there will only be one instance of exchangeName, no matter how many instances of SimulatedExchange you have. Make it non-static. Also, add this to instance variables in the constructor, such as this.exchangeName = exchangename;.

Upvotes: 5

Claudiu
Claudiu

Reputation: 1489

your exchangeName variable is static thus it belongs to the class rather to the SimulatedExchange instances.

if you remove static it should work;

public String exchangeName;

Upvotes: 1

Mureinik
Mureinik

Reputation: 311198

Your exchangeName member is static, meaning there's only a single instance for the class, not for each instance.

Each time you call the constructor, it's overridden. Just remove the static modifier to make it an instance variable, and you should be fine.

Edit: Same goes for your other members - Fee, buyLimit and sellLimit.

Upvotes: 3

Related Questions