0

the following function returns a memory address instead of the actual Node its supposed to, any input?:

public Node getNode(){
    Node nextnode = new Node(this.title, this.disX, this.disY);
    return nextnode;
}

When called such as:

AcNode aNode = new AcNode("Test", 0.5, 0.6)
System.out.println("See next node" + aNode.getNode());

AcNode is a subclass of Node, using the same constructor as a super. Any help?

Robin Green
  • 32,079
  • 16
  • 104
  • 187
czedarts
  • 51
  • 7
  • You should @Override toString() method. – Michu93 Apr 26 '18 at 12:14
  • See this: [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Jesper Apr 26 '18 at 12:20

1 Answers1

0

When you concatenate an object onto a String like you are doing there, the toString method will be called. By default the toString method will be called internally if you don't define a toString method, which will print a "memory address" pointing to the object. So you need to define a toString method in your Node class that returns the information you are looking for in that case.

Alternatively, you can call a getter instead of relying on the toString method:

aNode.getNode().getIdentifier()

or whatever your getter method is called (getIdentifier is just an example).

Robin Green
  • 32,079
  • 16
  • 104
  • 187