title: What causes variables from one class to be affected by another?
body: <p>I have a game I'm working on where a weapon, that belongs to a player, releases a projectile, in some cases, many projectiles. </p>
  
  <p>However, when I pass an Image Class I created, called a Sprite, from the weapon to the projectile, any scaling I do to that individual projectile's sprite will affect the Weapon's Sprite as if the Sprite object for the weapon was static (it's not).</p>
  
  <p>It starts from the weapon, in which I will post the relevant code for: </p>
  
  <pre><code>weaponProjectile[0] = new Pellet(releasedxCoor, releasedyCoor, mainChar.getDirection(), this, 0, projectileSprite);
      for(int i = 1; i &lt; weaponProjectile.length; i++) {
          float accuracy = randomFloat((float)-2.5, (float)2.5);
          weaponProjectile[i] = new Pellet(releasedxCoor, releasedyCoor, mainChar.getDirection(), this, accuracy, projectileSprite);
      }
  </code></pre>
  
  <p>Here is the Pellet Constructor: </p>
  
  <pre><code>public Pellet(float xCoor, float yCoor, int direction, Blunderbuss fromWeapon, float accuracy, Sprite projectile) {
      super(xCoor, yCoor, direction, fromWeapon);
      this.xCoor = xCoor;
      this.yCoor = yCoor;
  
      this.projectileSpeed = fromWeapon.getProjectileSpeed() + randomFloat(-2, 2);
      this.pierceMax = fromWeapon.getPierce();
      this.pierce = pierceMax;
      this.damage = fromWeapon.getDamage();
  
      this.direction = direction;
      this.accuracy = accuracy;
      this.moving = projectile;
  }
  </code></pre>
  
  <p>Here's where/how I scale images: </p>
  
  <pre><code>if(lifespan % 8 == 0) {
          this.projectileSpeed --;
          width--;
          height--;
          damage -= 2.5;
          this.moving.scaleWithRaster(0.9);
      }
  </code></pre>
  
  <p>The <code>scaleWithRaster</code> method:</p>
  
  <pre><code>public void scaleWithRaster(double scaleValue) {
      this.spriteSheet = imageScalar.getScaledImage(this.spriteSheet, scaleValue);
      this.xLength *= scaleValue;
      this.yLength *= scaleValue;
  }
  </code></pre>
  
  <p>and the <code>ImageScalar</code> class:</p>
  
  <pre><code>public BufferedImage getScaledImage(BufferedImage image, double scaleFactor) {
      this.affine.scale(scaleFactor, scaleFactor);
      this.op = new AffineTransformOp(affine, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
      image = op.filter(image, null);
  
      return image;
  }
  </code></pre>
  
  <p>The error I'm running into is the fact the raster for the image becomes too small nearly immediately, and the program crashes. Even if I shift the scale factor to 0.999999, it will still kill the program within a frame or so. </p>
  
  <p>I am certain the issue revolves around the Sprite object being copied, as I tested with a non-static and static counter, that was incremented each time the method was called, on the Sprite object in question, and the values for both were identical.</p>
  
  <p>I also know it is directly affecting the Weapon's sprite, as I had it draw the projectile from the weapon onto the screen, and it grew with the other projectiles. I have also tried setting it to final, which didn't prevent it form being scaled.</p>
  
  <p>I've ensured that nothing regarding these images is static or public, so I'm extremely confused as to how this is happening. I had originally thought that I got passed by value and pass by references for java mixed up, but that is not the case, and such, each Sprite object should be a copy of the weapon's Sprite object, no?</p>
  
  <p><strong>EDIT:</strong> Solution has been found. I had to create a copy method within the sprite object, as I didn't fully understand the rules of Java's pass by value. Thanks to those who helped!</p>