开发者

Confusion in Constructor Overloading Example

开发者 https://www.devze.com 2023-02-04 11:16 出处:网络
The following program gives output as I am Parameterized Ctor a = 0 b = 0 public class ParameterizedCtor {

The following program gives output as

 I am Parameterized Ctor
 a = 0
 b = 0

public class ParameterizedCtor {

    private int a;
    private int b;

    public ParameterizedCtor() {
        System.out.println("I am default Ctor");
        a =1;
        b =1;
    }

    public ParameterizedCtor(int a, int b) {
        System.out.println(" I am Parameterized Ctor");
        a=a;
        b=b;

    }
    public void print() {
        System.out.println(" a = "+a);
        Syst开发者_StackOverflowem.out.println(" b = "+b);
    }

    public static void main(String[] args) {

        ParameterizedCtor c = new ParameterizedCtor(3, 1);
        c.print();
    }

}

What is the reason?


The un-initialized private variables a and b are set to zero by default. And the overloading c'tctor comes into place.ie, parameterCtor(int a, int b) will be called from main and the local variables a & b are set to 3 and 1, but the class variables a and b are still zero. Hence, a=0, b=0 (default c'tor will not be called).

To set the class variable, use:

this.a = a;
this.b = b;


You need to do this:

public ParameterizedCtor(int a, int b) {
    System.out.println(" I am Parameterized Ctor");
    this.a=a;
    this.b=b;
}

otherwise, you're just re-assigning the a and b parameters to themselves.


this is called variable shadowing and default value of int is 0

make it like

 public ParameterizedCtor(int a, int b) {
        System.out.println(" I am Parameterized Ctor");
        this.a=a;
        this.b=b;
 }  

Also See

  • Something about this


use

this.a = a;
this.b = b;

instead of

a = a;
b = b;


use

this.a = a;
this.b = b;

instead of

a = a;
b = b;


This code

a=a;
b=b;

is assigning the value in 'a' to the parameter 'a'. What you intended is likely to be.

this.a=a;
this.b=b;

BTW: This shows as warning in my IDE.


You have a local variable called a and a member variable called a, so you need to use this.a to refer to the member variable, as a refers to the local variable.

It might be a better idea to rename the local variable so that it is not the same as the member variable.


public class thisDemo {
    public int x=1;
    public int y=2;

    String[] l=new String[1];String[] m=new String[1];String[] n=new String[1];

   public thisDemo(int a,int b)
           {
               this.x=14;
               this.y=4;
           }
   public thisDemo(String a[],String b[],String c[])
   {
       this.l[0]=a[0];
       this.m[0]=b[0];
       this.n[0]=c[0];
   }
   public thisDemo()
   {

   }
    public static void main(String[] args)
    {
        thisDemo thi=new thisDemo(2, 3);
        System.out.println(thi.getClass());
        System.out.println(thi.x+" "+thi.y);

        thisDemo td=new thisDemo();
        System.out.println(td.getClass());
        System.out.println("x="+td.x+"y="+td.y);

        String xA[]={"a"};
        String yA[]={"b"};
        String zA[]={"c"};
       thisDemo tsd=new thisDemo(xA,yA,zA);
       System.out.println(tsd.getClass());
       System.out.println(tsd.l[0]+" "+tsd.m[0]+" "+tsd.n[0]);
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消