Vinay
Vinay

Reputation: 3

initializing values to constructor

In the following code I have declared 2 user defined constructors, one without parameters and the other one with 3 parameters, in both the constructors I am assigning values to the instance variables and when the main method is executed the output for constructor without parameters is 2 and the o/p for constructor with 3 parameters is 0 in the first way, but when I try the second way the o/p for zero parameters constructor is 2 and for the 3 parameters constructor is 15, where I am passing the arguments while object creation, now I don't understand why in the first way the output is zero.

public class Main {

    int x, y, z;

    Main() {
        x = 2;
        y = 2;
        z = 2;
    }

    // first way
    Main(int x, int y , int z) {
        x = 20;
        y = 20;
        z = 10;
    }

    // second way
    Main(int x, int y , int z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public int sub() {
        int m;
        m = x + y - z;
        System.out.println("the value is " + m);
        return m;
    }
}

Following is the main method:

package demo;

public class Maintest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Main s = new Main();
        int s1 = s.sub();
        Main s3 = new Main(10,10,5);
        int s2 = s3.sub();
        System.out.println(s1);
        System.out.println(s2);
    }
}

Upvotes: 0

Views: 896

Answers (4)

Bhagyashree Ghushey
Bhagyashree Ghushey

Reputation: 16

I saw your code. When you use the constructor in //first way

you get the answer as 0 because the values 20, 20, 10 that you are initialising to variable x,y,z respectively gets initialised to constructor variable x,y,z not to the class variable x,y,z.

When you use constructor //second way

values get initialized to the class variables x,y,z because of the use of "this" keyword. this refers to the class variable hence when you use in second way constructors shows the result as 15.

Upvotes: -1

IfOnly
IfOnly

Reputation: 540

If instance variable name is similar to argument name, at that time use 'this' explicitly to solve the ambiguity issue for JVM.

Main(int x, int y, int z) {
    this.x = x;
    this.y = y;
    this.z = z;
}

This is the standard practice.

But if you change argument name then its not mandatory to use 'this'.

Main(int xx, int yy, int zz) {
    x = 20;
    y = 20;
    z = 10;
}

Here it refers to instance variables and will give you proper results.

Cheers!!!

Upvotes: 0

SachinSarawgi
SachinSarawgi

Reputation: 2692

this refers to current object reference on which the method is called. So using this before the variable refers to current object instance variable.

If you dont use this then they are pointing to the variable passed in the argument.

However if you changed function parameter name and then use only x it will refer to current object instance variable.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

When you use x=20 you are assigning a new value to the parameter x passed to the constructor, not assigning that value to instance member. When you write this.x, it clearly binds to the instance member.

If you still confusing about them, change the parameter names to some other and check.

Upvotes: 5

Related Questions