Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By Wnt2bsleepin

Thread: overloading toString methode

  1. #1
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default overloading toString methode

    hi
    I have some methods in one class .and I need toString method for all of them
    Can you help me?
    Thanks

  2. #2
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: overloading toString methode

    Where are you my friends?!!!!!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,787
    Blog Entries
    7
    Rep Power
    21

    Default Re: overloading toString methode

    A Class can only have one toString() method, however you can print whatever you want in any method.

    kind regards,

    Jos
    faraa likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: overloading toString methode

    Can you explain more?
    I don't understand how to use it...
    Thank's a lot

  5. #5
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: overloading toString methode

    You override it from the Object class. It allows you to print out a string representation of that class.


    Java Code:
    public class Main {
    	
    	public static void main(String[] args){
    		System.out.println("Creating class");
    		myInstance mi = new myInstance("Hello World", 21);
    		System.out.println(mi); 
    	}
    
    }
    
    class myInstance{
    	
    	private String myString;
    	private int myInt; 
    	
    	public myInstance(String myString, int myInt){
    		this.myString = myString;
    		this.myInt = myInt; 
    	}
    	
    	public String toString(){
    		return "myString: " + myString + "\nMyInt: " + myInt; 
    	}
    	
    }
    Output:
    Java Code:
    Creating class
    myString: Hello World
    MyInt: 21
    faraa likes this.

  6. #6
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: overloading toString methode

    Thank you so much
    but I have one class and in that class I have several methods.The program that I had written didn't work for all of them

  7. #7
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: overloading toString methode

    what do you mean?

    post your code so we can see what exactly you mean by "didn't work for all of them".

    if you have several Classes, you need a toString in every class, or have a super class that has a toString method.

  8. #8
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: overloading toString methode

    A toString method normally gives a string representation of a class. It doesn't matter how many methods in it. For instance:
    Java Code:
    public class Person{
    public Person(){
    this(null, null, 0, null);
    }
    
    public Person(String fName, String lName, int sSNumber, String dLiscence){
    this.fName= fName;
    this.lName= lName;
    this.sSNumber=sSNumber;
    this.dLiscence= dLiscence;
    
    }
    
    //I could have hundreds of methods to alter any or all of these attributes, but my toString method would work the same and display all information in a 
    //way I could use it.
    
    public String toString(){
    return String.format("Name: %s %s /n Social Security: %i and Driver Liscence : %s", fName, lName, sSNumber, dLiscence);
    }
    //all getter and setter methods below here
    
    
    private String fName;
    private String lName;
    private int sSNumber;
    private String dLiscence;
    
    }
    Last edited by jbarke12; 05-02-2013 at 09:52 PM.

  9. #9
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: overloading toString methode

    also if i remember correctly if you dont explicitly define a toString, but call a toString method doesnt it use the one from Object which prints the memory address?

    not really relevant but if OP encountered this problem they will know why they are getting a weird result.

Similar Threads

  1. JTextArea differenet methode of inserting text
    By taptaptill in forum AWT / Swing
    Replies: 3
    Last Post: 12-27-2012, 06:07 PM
  2. How to display toString() inside another toString()
    By SVTermiCobra in forum New To Java
    Replies: 1
    Last Post: 08-08-2012, 02:49 AM
  3. EOF methode
    By Dipke in forum New To Java
    Replies: 7
    Last Post: 10-02-2010, 04:50 PM
  4. using paint methode in another class
    By imorio in forum New To Java
    Replies: 8
    Last Post: 08-12-2010, 04:17 PM
  5. calling methode with array
    By koddy in forum New To Java
    Replies: 0
    Last Post: 06-30-2010, 02:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •