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

51,877 answers

573 users

How to use this to activate constructor chaining in Java

1 Answer

0 votes
public class Worker  
{  
    int id, age;   
    String name, profession;  
    
    public Worker(int age) {  
            this.age = age;  
            System.out.println("Worker(int age)");
        }  
    public Worker(int id, int age) {  
        this(age);  
        this.id = id; 
        System.out.println("Worker(int id, int age)");
    }  
    public Worker(int id, int age, String name, String profession) {  
        this(id, age);  
        this.name = name;   
        this.profession = profession; 
        System.out.println("Worker(int id, int age, String name, String profession)");
    }  
    
    public static void main (String args[]) {  
        Worker w1 = new Worker(983872, 56, "Dan", "Programmer");  
            
        System.out.println("id = " + w1.id + " Name : " + w1.name + " age = " + w1.age + 
                           " profession =  " + w1.profession);  
    }  
}  


/*
run:

Worker(int age)
Worker(int id, int age)
Worker(int id, int age, String name, String profession)
id = 983872 Name : Dan age = 56 profession =  Programmer

*/

 



answered Sep 27, 2019 by avibootz

Related questions

1 answer 142 views
1 answer 134 views
1 answer 149 views
1 answer 113 views
113 views asked Dec 12, 2020 by avibootz
1 answer 157 views
157 views asked Jul 25, 2019 by avibootz
...