Results 1 to 7 of 7
- 01-19-2011, 06:38 PM #1
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
toString() functionality mysterious
Hi all,
Please see the below code
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.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()); } } }
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?
- 01-19-2011, 06:50 PM #2
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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--
- 01-19-2011, 06:52 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-19-2011, 07:02 PM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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
- 01-19-2011, 10:09 PM #5
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.
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.So why isn't the overridden toString() in India ignored?
If you want to use the toString method of Object and not your own then remove it or rename it.
- 01-20-2011, 04:08 AM #6
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,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-20-2011, 04:40 AM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Similar Threads
-
Regarding Undo functionality for button
By bipul049 in forum New To JavaReplies: 1Last Post: 10-09-2010, 01:32 PM -
Split function Functionality
By Srinivasa in forum XMLReplies: 1Last Post: 10-08-2010, 02:24 AM -
JComboBox Functionality
By Moncleared in forum AWT / SwingReplies: 1Last Post: 02-19-2009, 07:57 PM -
Functionality of Buttons
By ljk8950 in forum AWT / SwingReplies: 6Last Post: 08-15-2008, 01:44 PM -
Change Password Functionality
By Priyadharshini.s in forum Advanced JavaReplies: 5Last Post: 04-02-2008, 12:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks