Search code examples
javaclassinstanceinstance-variables

Why is Java assigning the parameters of my second instance to both instances of a class?


I am in an entry level college programming class and this is perhaps a novice question, but I haven't been able to find the answer elsewhere and can't figure out why Java is assigning the parameters from my second instance of Conveyor to both instances.

Here is my Conveyor class:

    public class Conveyor
    {
       //fields
       private static String type;
       private static double speed; //Speed is measured in m/s (meters/second)
       
       //constructors
       public Conveyor(String type, double speed)
       {
          this.type = type;
          this.speed = 0;
          this.speed = setSpeed(speed);
       }
       
       //methods
       public String getType()
       {
          return this.type;
       }
       
       public double getSpeed()
       {
          return this.speed;
       }
       
       public double setSpeed(double speed)
       {  
          if(speed <= 0)
          {
             this.speed = this.speed;
          }
          else
          {
             this.speed = speed;
          }
          
          return this.speed;
       }
       
       public double timeToTransport(double distance)
       {
          double t = distance / getSpeed();
          return t;
       }
    }

And the Conveyor App being used to test it:

    public class ConveyorApp
    {
       public static void main(String[] args)
       {
          Conveyor c1 = new Conveyor("flat belt",0.9);
          Conveyor c2 = new Conveyor("roller", -0.5);
          
          System.out.printf("Conveyor 1: %s conveyor with a speed of %.1f\n", c1.getType(), c1.getSpeed());
          System.out.printf("Time to transport an item 50m: %.1f\n\n", c1.timeToTransport(50));
          
          System.out.printf("Conveyor 2: %s conveyor with a speed of %.1f\n", c2.getType(), c2.getSpeed());
          System.out.printf("Time to transport an item 50m: %.1f\n\n", c2.timeToTransport(50));
       }
    }

If I move the second instance below the print statements of the first it produces the expected behavior and prints:

    Conveyor 1: flat belt conveyor with a speed of 0.9
    Time to transport an item 50m: 55.6
    
    Conveyor 2: roller conveyor with a speed of 0.0
    Time to transport an item 50m: Infinity

Otherwise in the order presented above this is my output:

    Conveyor 1: roller conveyor with a speed of 0.0
    Time to transport an item 50m: Infinity
    
    Conveyor 2: roller conveyor with a speed of 0.0
    Time to transport an item 50m: Infinity

What am I doing wrong?


Solution

  • The fields for your Conveyor class are marked as static fields, but it sounds like you want them to behave as instance fields.

      public class Conveyor
        {
           //fields
           private String type;
           private double speed; //Speed is measured in m/s (meters/second)
           ...
           
    

    Just removing the static keyword like this should be all you need to do!