Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Valid toString method?

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Valid toString method?

    Hi

    Can anyone recognise if this is a valid toString method?
    public String toString()
    {
    System.out.println("Tom ");
    }
    cheers
    tom


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Valid toString method?

    No.

    Hint: What are you returning? What is the return type of the toString method? When you call a standard class's toString() function, does anything print out? Or do you have to print that out yourself?

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Valid toString method?

    Quote Originally Posted by dcshoecousa View Post
    Hi

    Can anyone recognise if this is a valid toString method?
    public String toString()
    {
    System.out.println("Tom ");
    }
    cheers
    tom
    No. It has to return a String.

    A valid toString() method would

    public String toString()
    {
    String str = "Tom";
    return str;

    }

    What you're doing with yours above is basically

    public void toString()
    {

    System.out.println("Tom");

    }

    Also, it depends what you mean by "valid".

    Object has a toString() method that returns a String, but it makes no sense whatsoever unless you're a java compiler (like Eclipse). Also, since Object has a toString(), unless you overload it, you have to have the same parameters, in this case none.

    All classes have Object's toString() in them by default and keep it as theirs unless they override it with their own.

    An ArrayList will return a toString like this:

    [item one, item two, item three, ... last item]

    However, it will again make sense only for classes that have a "nice" toString(), like String or Integer for example, otherwise it'll probably use Object's toString(). Overloading is for when you want to have a different parameter, but still have the same return type.

    For instance:

    public String toString(String example)
    {

    return example;

    }

    or

    public String toString(String secondExample)
    {

    String str = "bob " + secondExample;

    return str;

    }



    Also, for a toString(), often people use them for things like:

    public String toString()
    {
    String str = "The value is: " + getValue() + ". The name of this item is: " + getName() + ".";

    // be careful to use getValue() and getName() and other get methods instead of
    // having String str = "The value is: " + value + ". The name of this item is: " + name + ".";
    // that may not always produce what you want
    // also, make sure that if you have a get method, you call a setMethod() either in your constructor
    // or in this toString() method. If in the toString() method itself, make sure to have the value you're
    // passing it as a parameter, set Methods have at least 1, sometimes more, parameters, is defined somehow
    // inside the method or else it'll say "cannot find symbol". Also, if you don't call the set method, your
    // toString() will return "null" for all the get Methods that never called a set method.

    return str;
    }

Similar Threads

  1. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  2. Help with toString and accesing user input
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 07:58 PM
  3. [SOLVED] Using class implicit toString() for array index
    By Quetzalma in forum Java Theory & Questions
    Replies: 2
    Last Post: February 3rd, 2010, 04:04 PM
  4. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 05:44 AM
  5. ClassCastException in Double Linked List toString
    By Rastabot in forum Collections and Generics
    Replies: 2
    Last Post: April 24th, 2009, 11:48 AM