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 7 of 7

Thread: toString problem

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question [SOLVED] toString problem

    Hi, i need to call a toString method in another one of my toString methods, however i am getting a compiling erroe each time, can anyone look at the code below and let me know what i have done wrong?

    Thanks in advance.


    (Contact class)
    public String toString()
            {
                String output = "First Name: " + fName + "\n" + "Second Name: " + sName + "\n" + "Street " + street + "\n" + "Town: " + town + "\n" + "Postcode: " + postcode;
     
                return output;
     
            }



    (AddPersonalContact class)

    public class AddPersonalContact extends Contact
    .
    .
    .
    .
    .
    public String toString()
        {
            String output = Contact.toString() + "\n" + "Phonenumber: " +  phonenumber;
            return output;
     
        }


    **EDIT**

    Sorry, forgot the compiling error:

    non-static method toString() cannot be referenced in a static context
    Last edited by 93tomh; July 29th, 2012 at 04:35 AM.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: toString problem

    Quote Originally Posted by 93tomh View Post
    ...compiling error:

    non-static method toString() cannot be referenced in a static context
    If you are calling a function in the same class as your static void main(), you probably don't have an object, so the function must be static:
    public class Z
    {
        public static void main(String [] args)
        {
            foo();
        }
        // Since there is no "Z object" in this program, the function
        // must be static.
        static void foo()
        {
            System.out.println("foo() to you!");
        }
    }

    Speaking from recent personal experience, I have found that there are a number of things like this in Java that "take a little getting used to." Before coming to a complete understanding of everything that is involved, I decided to accept certain things as idioms, with complete illumination to come a little later.


    Cheers!

    Z

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: toString problem

    If you are calling a function in the same class as your static void main(), you probably don't have an object, so the function must be static:
    I dont have a static void main() in any of my classes, is this the reason it is not working, or am i completely off the ball. Sorry I'm not too great with java but im trying to understand it

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: toString problem

    Quote Originally Posted by 93tomh View Post
    ... is this the reason it is not working...
    Well I was guessing. Now I am guessing that my guess was wrong.

    If you want more, you have to give more.

    Here is my suggestion: Post enough code so that we can compile it and then maybe we won't have to guess.


    Cheers!

    Z
    Last edited by Zaphod_b; July 28th, 2012 at 10:49 AM.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: toString problem

    Sorry, i thought those parts of the code were sufficient. Here are the full classes.


    public class Contact
    {
        protected String fName;
        protected String sName;
        protected String street;
        protected String town;
        protected String partOnePC;
        protected String partTwoPC;
        protected String postcode;
     
     
        public Contact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC)
        {
     
           this.fName = fName;
            this.sName = sName;
            this.street = street;
            this.town = town;
     
     
     
     
     
     
     
            if(partOnePC != null && (partOnePC.length() >= 3 && partOnePC.length() <= 4))
            {
                this.partOnePC = partOnePC;
     
     
            }
            else
            {
              System.out.println("Please insert a string with 3-4 characters for part one postcode");  
            }
     
            this.partOnePC = partOnePC;
            if(partTwoPC != null && (partTwoPC.charAt(2) > 0 || partTwoPC.charAt(3) > 0) && partTwoPC.length() < 4)
            {
               this.partTwoPC = partTwoPC;
            }
            else
            {
                System.out.println("Please insert a string with 3-4 characters for part two postcode");
            }
     
            this.postcode = partOnePC + " " + partTwoPC; 
        }
     
            public String toString()
            {
                String output = "First Name: " + fName + "\n" + "Second Name: " + sName + "\n" + "Street " + street + "\n" + "Town: " + town + "\n" + "Postcode: " + postcode;
     
                return output;
     
            }


    [CODE
    public class AddPersonalContact extends Contact
    {

    public String contactType;
    public String phoneNumber;

    public AddPersonalContact(String fName, String sName, String street, String town, String partOnePC, String partTwoPC, String phoneNumber)
    {
    super(fName, sName, street, town, partOnePC, partTwoPC);

    this.phoneNumber = phoneNumber;
    this.contactType = "Personal";
    }


    public String toString()
    {

    String output = Contact.toString() + "\n" + "Phonenumber: " + phonenumber;
    return output;

    }

    }


    [/CODE]

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: toString problem

    If you want to use the Contact toString() method in a subclass, you can do something like this:
    // AddPersonalContact subclass of Contact class
    public String toString()
    {
        // Use the toString() method in Contact
        String output = super.toString() + whatever...
        return output;
    }

    The "super" keyword is a way to get to a parent's non-static, non-private methods or variables from a subclass.




    Reference: Using the Keyword super


    In fact, I could have gleaned enough information from your original post to come up with this, but my "guess" was a knee-jerk reaction based on previous experience. In the future I'll try to pay attention rather than jumping to conclusions...



    Cheers!

    Z
    Last edited by Zaphod_b; July 28th, 2012 at 10:59 AM.

  7. The Following User Says Thank You to Zaphod_b For This Useful Post:

    93tomh (July 28th, 2012)

  8. #7
    Member
    Join Date
    Jul 2012
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: toString problem

    Fantastic! Thank you very much, seems obvious to me now haha. And dont worry, its my fault for thinking that little snippets would be enough for someone to see what was wrong with my code

    Thanks a bunch!

Similar Threads

  1. Can I use toString() here?
    By titowinky in forum Java Theory & Questions
    Replies: 2
    Last Post: May 30th, 2012, 07:15 AM
  2. Can someone help me understand toString()?
    By Psychotron in forum Java Theory & Questions
    Replies: 10
    Last Post: January 18th, 2012, 09:43 PM
  3. Why cant I use toString() with vector?
    By tarkal in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2011, 07:24 AM
  4. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 05:44 AM