title: When is toString() method called
body: <p>I got an example to override the toString() method.</p>
  
  <pre><code>import java.util.Scanner; 
  
  public class Demo{ 
  
      String name = "";
      String age = "";
  
      Demo(String name, String age){
              this.name = name;
              this.age = age;
          }
  
  
      public static void main(String[] args) {  
          Scanner scanner = new Scanner(System.in);  
  
          System.out.print("Enter the name:");  
          String name = scanner.next();  
          System.out.print("Enter the age:");  
          String age = scanner.next();  
  
          Demo test = new Demo(name, age);  
          System.out.println(test);  
  
      }  
  
      public String toString() {  
          return ("Name=&gt;" + name + " and " + "Age=&gt;" + age);  
      }  
  }  
  </code></pre>
  
  <p>My doubt is, how is it printing test as Name=>abc and Age=>12 even when toString() method is not being called neither from constructor nor from main?</p>