Overriding the ToString Method __object

Source: Internet
Author: User
When you want to read some useful details about an object, you can invoke ToString () on the object. For example, when an object reference is passed to System.out.println (), the object's ToString () method is invoked. All classes in Java inherit from the object parent class, so we can just rewrite the ToString () method in the class to show the information we want. Look at the following example: public class Overridetostring {public String toString () {return ' This is overridetostring! '     ; } above we rewrote the ToString method, and now we test: public class Testoverridetostring {public static void main (string[] args) {O                  verridetostring ots = new overridetostring ();     System.out.println (OTS); The output result is: This is overridetostring!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.