Rincy
Rincy

Reputation: 1

copy constructor clarification needed

public class CopyConstructorEx 
{
    String web, webb;

    CopyConstructorEx(String w){ 
        web = w;       }         

    CopyConstructorEx(CopyConstructorEx je){    
            webb = je.web;         }  

    void disp(){        
         System.out.println("Website: "+web);      }        

    public static void main(String args[]){

        CopyConstructorEx obj1 = new CopyConstructorEx("BeginnersBook");                

        CopyConstructorEx obj2 = new CopyConstructorEx(obj1);           

        obj1.disp();
        obj2.disp();       
   }      
}    

output:

Website: BeginnersBook

Website: null

Can anyone explain why second output is null?

Upvotes: 0

Views: 33

Answers (1)

shree.pat18
shree.pat18

Reputation: 21757

web being a string type variable is null by default. In your copy constructor, you aren't assigning anything to it, so there's no reason for it to change.

Upvotes: 2

Related Questions