I am an intermediate Java beginner and also completely new to stack overflow. (This is my first post.)

I have a question about the following code and the assignment of values to a reference.

First, the code:

import java.awt.Point;

public class DrawPlayerAndSnake
{
  static void initializeToken( Point p, int i )
  {
    int randomX = (int)(Math.random() * 40); // 0 <= x < 40
    int randomY = (int)(Math.random() * 10); // 0 <= y < 10
    p.setLocation( randomX, randomY );
    /*
    System.out.println("The position of the player is " + playerPosition + ".");
    i = i + randomX;
    System.out.println(" i lautet " + i + ".");
    */
    Point x = new Point(10,10);
    System.out.println("The position of the x is " + x + ".");
    System.out.println("The position of the p is " + p + ".");    
    p.setLocation(x);
    x.x = randomX;
    x.y = randomY;
    p = x;
    System.out.println("The position of the p is now" + p + ".");
    System.out.println("The x position of the p is now " + p.getX() + ".");  

  }

  static void printScreen( Point playerPosition,
                       Point snakePosition )
  {
    for ( int y = 0; y < 10; y++ )
    {
      for ( int x = 0; x < 40; x++ )
      {
        if ( playerPosition.distanceSq( x, y ) == 0 )
          System.out.print( '&' );
        else if ( snakePosition.distanceSq( x, y ) == 0 )
          System.out.print( 'S' );
        else System.out.print( '.' );
      }
      System.out.println();
    }
  }

  public static void main( String[] args )
  {
    Point playerPosition = new Point();
    Point snakePosition  = new Point();

    System.out.println( playerPosition );
    System.out.println( snakePosition );
    int i = 2;
    initializeToken( playerPosition , i );
    initializeToken( snakePosition, i);

    System.out.println( playerPosition );
    System.out.println( snakePosition );

    printScreen( playerPosition, snakePosition );
  }
}      

This code is modified for educational purposes (I am trying to understand this). The original code is out of the book "Java ist auch eine Insel" by Chrisitan Ullenboom.

Okay, now there is this method intializeToken and I am passing over an instance of the class Point. (I hope i get that right, so if I make a mistake please feel free to correct me.) When this method is called by the main method, a new reference to - say - the instance playerPosition is created by Point p. Now, because the parameter playerPosition which is passed over to method initializeToken is not final I can do whichever assignment to point p that I want.

But when I create a new point object with the reference variable x and assign this reference to p by p = x; the x and y position of playerPosition won't change, but by p.setLocation() it does.

Can anyone tell me why?

有帮助吗?

解决方案

The Point p passed into initializeToken is a local reference to the instance of p stored in memory. When you call p.setLocation(), you are "dereferencing" p, that is, modifying the actual instance in memory. However, if you simply set p = new Point(x, y), you are modifying the local variable in the initializeToken method, which will disappear once the method terminates.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top