name of the class

I need to have the name of my class as the first line of my toString m

Page 1 of 1

3 Replies - 678 Views - Last Post: 03 March 2008 - 12:55 AM Rate Topic: -----

#1 shadow_666  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-January 08

name of the class

Posted 01 March 2008 - 08:46 AM

Please, I need help. I have a class name Vehicle and I need to print the string representation of my object. However, the first line in my toString output must be the name of the class. Question, there is a method to call so it gives me the name of the class ???

public Vehicle(String name, String address, String phone, String email){

ownerName = name;
onwerAddress = address;
ownerPhone = phone;
ownerEmail = email;

dateCreated = new java.util.Date();

}//end constructure

//toString method returns a string representation of this object
public String toString(){

return ????

}//end of toString method

}//end Vehicle class

the output of my toString method should look like this:

Vehicle (it is the name of the class, or what type of object it is)
name
address
phone
email

Is This A Good Question/Topic? 0
  • +

Replies To: name of the class

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: name of the class

Posted 01 March 2008 - 08:52 AM

yep there is one..

public String toString() {
		return this.getClass()+"\n";
	}


This post has been edited by letthecolorsrumble: 01 March 2008 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 shadow_666  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-January 08

Re: name of the class

Posted 01 March 2008 - 08:58 AM

View Postletthecolorsrumble, on 1 Mar, 2008 - 08:52 AM, said:

yep there is one..

public String toString() {
		return this.getClass()+"\n";
	}




thank you so much ....
Was This Post Helpful? 0
  • +
  • -

#4 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Re: name of the class

Posted 03 March 2008 - 12:55 AM

View Postshadow_666, on 1 Mar, 2008 - 09:28 PM, said:

View Postletthecolorsrumble, on 1 Mar, 2008 - 08:52 AM, said:

yep there is one..

public String toString() {
		return this.getClass()+"\n";
	}




thank you so much ....



Just to make it clear...
toString method shown above is defined in Object class, which is superclass of all the classes and hence is available to all the classes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1