Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java Question

Name: Anonymous 2013-09-19 6:47

Small problem with changing values in another class then viewing said change.

Ship class:

public class Ship {
  int xOffset = 0;

  String toString {
    System.out.println(xOffset);
  }
}

TestShip Class

public class TestShip {
  public static void main(String[] args) {
    Ship ship = new Ship();
    ship.xOffset = 1;
   
    system.out.println(ship.toString);
    system.out.println(ship.xOffset);
}

The first out put gives me 0, the second output gives my 1. Why?

Name: DarrHiggs 2013-09-19 7:41

You need to reference the current object.

`System.out.println(this.xOffset);`

Name: Anonymous 2013-09-19 13:08

Change system.out.println(ship.toString)
to System.out.println(ship)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List