i have several Constructor in the class
public class Date {
...
public Date(first case)
...
public Date(second case)
...
public Date(third case)
...
public String toString(){
...}
How to write the code in toSTring() ????
Can any one explain to me
What your
toStringreturns is not decided by how manyconstructoryou have in your class. They are used toreturnaStringrepresentation that you want to be printed when you print the instances of your class. You can return any field or concatenation of some fields from it.So, if you have a
Personclass with some4-5 fieldsincludingidandnameand you want to print theidandnameof the person separated by a colon, when you print the instanse, yourtoString()would look like:-So, if you have an instance of
Personclass withid = 5,name = rohit,age = 23and someemail id, then when you display that instance: –The above statement will print: –
as output.
But you can of course put a condition in your
toStringto check whether a field is empty or not, and you can return a message accordingly.