0
public class example 
{
    private String one;
    private String two;
    public example(String one, String two)
    {
        this.one = one;
        this.two = two;
    }

    public static void main(String[] args)
    {
        System.out.println(returning());
    }

    public static example returning()
    {
        return new example("yes", "no");
    }

    public String toString()
    {
        return one + " SPACESPACESPACE " + two;
    }
}

run:
yes SPACESPACESPACE no
BUILD SUCCESSFUL (total time: 3 seconds)

I was trying to learn how constructors work, and I wrote some code that had a toString method. I called System.out.print on the returning() function, and it printed "yes SPACESPACESPACE no" which is only found in the toString method. However, I never had an object for the toString method to act on (namely objectCreatedFromClass.toString()), so I never actually used the toString method.

Can someone explain to me why this happened? Why was I allowed to return a new object, namely return new example("yes", "no") when my class is a static method?

7
  • 2
    How do you think System.out.println turns objects into strings that it can print out?
    – Ixrec
    Commented Mar 15, 2016 at 22:24
  • But when you System.out.println an object, wouldn't it give you the getClass().getName() + '@' + Integer.toHexString(hashCode()) instead? But somehow I get a toString of that? Which is interestingly odd? Commented Mar 15, 2016 at 22:27
  • 2
    I believe you just described the default implementation of Object.toString().
    – Ixrec
    Commented Mar 15, 2016 at 22:30
  • 1
    @gordonsung have you tried stepping through the code in a debugger?
    – user22815
    Commented Mar 15, 2016 at 22:40
  • 1
    @BarryTheHatchet You are right. I apologize. Commented Mar 16, 2016 at 0:03

3 Answers 3

5

println follows convention and calls your object's toString() when it needs to, in order to obtain a string from some class it doesn't know about.

The default Object.toString() implementation gives you getClass().getName() + '@' + Integer.toHexString(hashCode()), but you've overridden it with your own version. You're seeing the results of that version.

0
3

Why was I allowed to return a new object, namely return new example("yes", "no") when my class is a static method?

Constructors can be called by anything that knows about them (via import foo.bar.MyClass; or part of the same file), and has access to them (public or same class). Since returning() is part of your example class, it can call its constructors.

Note that if static methods could not call constructors, we'd be in trouble. The first method that runs in a Java program is main(), which is static. If main() could not call any constructors, we could never construct any objects.

0

You can delete your returning method. And create and example object in main method: example n1=new example("yes","no"); System.out.println(n1);

It suppose to work! So if you will not write a method toString here your system will print out only address where your object store. But if you do a method it will print your String.

It will be better if you will write your contractor and main method in different classes, it will make your code clear :):)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.