Results 1 to 7 of 7
  1. #1
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default toString() functionality mysterious

    Hi all,

    Please see the below code

    Java Code:
    class India {
    
    	String state;
    	String capital;
    	int noOfDists;
    	String vehReg;
    
    	India (String state, String capital, int noOfDists, String vehReg) {
    		this.state = state;
    		this.capital = capital;
    		this.noOfDists = noOfDists;
    		this.vehReg = vehReg;
    	}
    	
    	//this is where i find a problem. I don't know who is calling it?
    	public String toString() {
    		return state + ":" + capital + ":" + noOfDists + ":" + vehReg; 
    	}
    }
    
    class MyCollection {
    	public static void main (String args[]) {
    		Collection<India> col1 = new ArrayList<India>();
    		
    		//adding 4 India objects to the collection
    		col1.add(new India("Karnataka", "Bangalore", 30, "KA"));
    		col1.add(new India("Kerala", "Trivandrum", 14, "KL"));
    		col1.add(new India("Andra Pradesh", "Hyderabad", 23, "AP"));
    		col1.add(new India("Tamil Nadu", "Chennai", 32, "TN"));
    
    		Iterator iter1 = col1.iterator();
    		while (iter1.hasNext()) {
    			System.out.println(iter1.next());
    		}
    	}
    }
    It's compiling. But the console output of iter1.next() is exactly what is returned by toString() method in India class. It is shown below.

    Karnataka:Bangalore:30:KA
    Kerala:Trivandrum:14:KL
    Andra Pradesh:Hyderabad:23:AP
    Tamil Nadu:Chennai:32:TN

    If the toString() is not defined I get this output
    India@19821f
    India@addbf1
    India@42e816
    India@9304b1

    Now my question is How/Why is the implicit call of toString() happening?
    Is it something related to overriding of toString()? This is just my guess. Even if it is correct could anyone please explain in detail, especially who is calling that method even though it is overridden?

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hi - you are correct, it has everything to do with the overriding of toString(). Every class you create in java extends the Object class, and inherits the default toString() method. By default, if you try to print out an object using System.out.println, it called the Object class' toString() method which simply prints the name of the object (India), followed by the @ sign and the location in memory where the reference to the object is stored.

    Often times, this is not the desired output. In order to print out something meaningful, you can override the default toString() method by providing an implementation on your own (as you have done in your India class). Therefore, whenever you are in an instance of India, and you use System.out.printn(India object) to print to the console, it will use your version of the toString() method.

    Hope that helps.
    --user0--

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

    Default

    That's how PrintStreams/PrintWriters work: when the have to print an object (any type of object) they call the .toString() method first and print that. A very handy feature indeed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Hi User0,

    Thanks for the clarification. Now I tried something else. I changed the access specifier of toString() to private. Now i'm getting compiler error "attempting to assign weaker access privileges"
    Anyway outsiders don't have access to private members. So why isn't the overridden toString() in India ignored?
    why can't it call Object class' toString() method and give this output
    India@19821f
    India@addbf1
    India@42e816
    India@9304b1

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The API is your friend. If you had read it you would see that the println(object) method calls String.valueOf(object) method, which calls object.toString() method.

    So why isn't the overridden toString() in India ignored?
    Because you got a compiler error and the error told you everything. Since the toString method in Object class is public you cannot make the access weaker, that is you cannot make your toString method private or protected. It has to be public.

    If you want to use the toString method of Object and not your own then remove it or rename it.

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    There are some method overriding rules that you are supposed to follow in order to keep the compiler happy.

    Go through this link and have a look at them : Method Overriding Rules

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    thanks goldest for the link. Your replies were always helpful.
    Got a convincing answer now. :)

Similar Threads

  1. Regarding Undo functionality for button
    By bipul049 in forum New To Java
    Replies: 1
    Last Post: 10-09-2010, 01:32 PM
  2. Split function Functionality
    By Srinivasa in forum XML
    Replies: 1
    Last Post: 10-08-2010, 02:24 AM
  3. JComboBox Functionality
    By Moncleared in forum AWT / Swing
    Replies: 1
    Last Post: 02-19-2009, 07:57 PM
  4. Functionality of Buttons
    By ljk8950 in forum AWT / Swing
    Replies: 6
    Last Post: 08-15-2008, 01:44 PM
  5. Change Password Functionality
    By Priyadharshini.s in forum Advanced Java
    Replies: 5
    Last Post: 04-02-2008, 12:56 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
  •