Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to override the ToString() method in Java

1 Answer

0 votes
public class Worker {  
    int id;  
    String name;  

    Worker(int id, String name) {  
        this.id = id;  
        this.name=name;  
    }  
    
    // overriding toString()
    public String toString() {
      return id + " " + name;
    }  
    
    public static void main(String args[]) {  
        Worker w1 = new Worker(12432, "Liam");  
        Worker w2 = new Worker(37473, "Emma");  
         
        System.out.println(w1.toString());
        System.out.println(w1);
       
        System.out.println(w2);
    }  
}      



/*
run:

12432 Liam
12432 Liam
37473 Emma
	
*/

 



answered Oct 5, 2019 by avibootz

Related questions

2 answers 203 views
203 views asked Apr 25, 2017 by avibootz
1 answer 165 views
165 views asked Jan 13, 2017 by avibootz
1 answer 150 views
150 views asked Nov 9, 2021 by avibootz
1 answer 165 views
1 answer 104 views
104 views asked Oct 16, 2022 by avibootz
1 answer 139 views
139 views asked Dec 11, 2020 by avibootz
...