-1
class Tank{
    int level;
}
class aliasing{
    public static void main(String args[]){
        Tank t1 = new Tank();
        Tank t2 = new Tank();

        t1.level=21;
        t2.level=32;
        System.out.println("t1: " + t1 + " t2: " + t2);
    }
}

This block of code produces the output: t1: Tank@1b4b24d t2: Tank@260829. Obviously this is wrong, but i don't know why all of a sudden all my code is producing nosense. Also, if i just intiliaze a primitive value i can print that out no problem with the correct value, so i don't know why only objects are messing up.

1
  • What output do you expect and why? Commented Nov 3, 2014 at 19:22

2 Answers 2

1

You need to override the toString() method in Tank class to produce meaningful representation of your object.

Sign up to request clarification or add additional context in comments.

Comments

0

It is printing the correct output. As can be seen in Object.toString() documentation, the default is to print the name of the class, an @ character and the hash of the specific object.

The hashes both differ as they are two different objects. If you were to set t1 = t2 = new Tank(); then you would see the hashes match up as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.