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,856 questions

51,777 answers

573 users

How to use copy constructor to copy the values of one object into another with Java

1 Answer

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

    worker(int n, String nm){  
        id = n;  
        name = nm;  
    }  
    
    // copy constructor 
    worker(worker w) {  
        id = w.id;  
        name =w.name;  
    }
    
    void print() {
        System.out.println(id + " " + name);
    }  
   
    public static void main(String args[]) {  
        worker w1 = new worker(876234, "Dan");  
        worker w2 = new worker(w1); 
        
        w1.print();  
        w2.print();  
   }  
}  



/*

run:

876234 Dan
876234 Dan

*/

 



answered Sep 26, 2019 by avibootz

Related questions

1 answer 155 views
1 answer 134 views
1 answer 134 views
1 answer 141 views
141 views asked Apr 28, 2018 by avibootz
2 answers 222 views
222 views asked Apr 28, 2018 by avibootz
2 answers 249 views
...