-5

I have a simple Java program to generate a random dice facing - code below:

Dice facing class

class Dice
{
    private int face;

    public Dice()
    {
        System.out.println(face());
    }

    public int getFace()
    {
        return face;
    }

    public void setFace(int face)
    {
        this.face = face;
    }

    private int face()
    {
        face = (int)(Math.random() * 7);
        return face;
    }
}

Main class

class John
{
    public static void main (String [] args)
    {
        Dice dice = new Dice();
        System.out.println(dice);
    }
}

When I do a println, it returns this:

5
Dice@6d06d69c

I have no idea why there is a Dice@6d06d69c. Why will this appear and how can I modify it to prevent it from appearing?

Thank you.

SunnyBoiz
  • 514
  • 1
  • 5
  • 14

1 Answers1

0

System.out.println(dice);

Java invokes here "toString()" method of Dice class

Dice@6d06d69c

You dont provide implementation of your own toString for Dice class so default implementation of toString from Object-class is invoked.

If You are more interested please see java documentation: https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--