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?

closed as unclear what you're asking by Andres F., gnat, user22815, Ixrec, Bart van Ingen Schenau Mar 21 '16 at 11:47

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    How do you think System.out.println turns objects into strings that it can print out? – Ixrec Mar 15 '16 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? – gordon sung Mar 15 '16 at 22:27
  • 2
    I believe you just described the default implementation of Object.toString(). – Ixrec Mar 15 '16 at 22:30
  • 1
    @gordonsung have you tried stepping through the code in a debugger? – user22815 Mar 15 '16 at 22:40
  • 1
    @BarryTheHatchet You are right. I apologize. – gordon sung Mar 16 '16 at 0:03
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.

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 :):)

Not the answer you're looking for? Browse other questions tagged or ask your own question.