Thursday 12 July 2012

How to override toString() Method in java

 toString () method in java

This article is dedicated to those who are new to java world and not aware much about functionality of toString () method of java .whenever we work in real time application the first thing we do after start the development of code we override the to string method, its not compulsory or mandatory but if we want to provide more concise and informative representation of our object we should override this method. So let’s first discuss what the purpose behind it is.

Syntax is String toString ()

  • As we know it is defined in object class for the purpose of string representation of any object which we created.
  • It helps developer in debugging the application in development process.
  • Its is mostly used for logging the application and provide simple and convenient way to represent  the messages
  • Whenever we deal with exception and errors using this method we can pass informative messages to users.
  • The default tostring method display Class Name, “@”, and the hex version of the object’s hash code concatenated into a string.
  • toString () method is internally called by value Of () method for Object s.
  • Explicitly No need to call this method its  is automatically invoked when  an object is used in a concatenation expression or in a call to print( ).

package parser;

import java.util.Date;

public class StocktoStringTest {
     
            private String symbol;
            private String exchange;
            private long lotSize;
            private int tickSize;
            private Date settlementDate;
            private float price;
           

      public String getSymbol() {
                  return symbol;
            }

            public void setSymbol(String symbol) {
                  this.symbol = symbol;
            }

            public String getExchange() {
                  return exchange;
            }

            public void setExchange(String exchange) {
                  this.exchange = exchange;
            }

            public long getLotSize() {
                  return lotSize;
            }

            public void setLotSize(long lotSize) {
                  this.lotSize = lotSize;
            }

            public int getTickSize() {
                  return tickSize;
            }

            public void setTickSize(int tickSize) {
                  this.tickSize = tickSize;
            }

     

            public Date getSettlementDate() {
                  return settlementDate;
            }

            public void setSettlementDate(Date settlementDate) {
                  this.settlementDate = settlementDate;
            }

            public float getPrice() {
                  return price;
            }

            public void setPrice(float price) {
                  this.price = price;
            }

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            StocktoStringTest stockTest=new StocktoStringTest();
           
            stockTest.setSymbol("symbolTest");
            stockTest.setExchange("NSE");
            stockTest.setLotSize(1000);
            stockTest.setTickSize(100);
            stockTest.setSettlementDate(new Date());;
            stockTest.setPrice(2000);
            System.out.println(stockTest);
           
      }

      @Override
      public String toString() {
            return "StocktoStringTest [symbol=" + symbol + ", exchange=" + exchange
                        + ", lotSize=" + lotSize + ", tickSize=" + tickSize
                        + ",  settlementDate="
                        + settlementDate + ", price=" + price + ", getSymbol()="
                        + getSymbol() + ", getExchange()=" + getExchange()
                        + ", getLotSize()=" + getLotSize() + ", getTickSize()="
                        + getTickSize() + ", getSettlementDate()=" + getSettlementDate()
                        + ", getPrice()=" + getPrice() + ", getClass()=" + getClass()
                        + ", hashCode()=" + hashCode() + ", toString()="
                        + super.toString() + "]";
      }
     

}

No comments:

Post a Comment