this Keywordnew - Copy.pdf

download this Keywordnew - Copy.pdf

of 6

Transcript of this Keywordnew - Copy.pdf

  • 7/25/2019 this Keywordnew - Copy.pdf

    1/6

    thisKeywordthisis a reference to the current object,within an instance method or a constructorthe

    object whose method or constructor is being called. We can refer to any member of the

    current object from within an instance method or a constructor by using this.

    thiscannot be used with static methods.

    Usage of this keyword

    1. this keyword can be used to refer current class instance variable.

    2. this() can be used to invoke current class constructor.

    3. this keyword can be used to invoke current class method (implicitly)

    4. this can be passed as an argument in the method call.

    5. this can be passed as argument in the constructor call.

    6. this keyword can also be used to return the current class instance.

    1) The this keyword can be used to refer current class instance variable.If there is ambiguity between the instance variable and parameter, this keyword resolves

    the problem of ambiguity.

    In General if we have a class instance variable x of a data type y and we have a method in

    which we pass another variable x of same data type to it as a parameter the compiler

    wont be able to understand which variable x we are referring to. So we use this.

    Eg 1)

    Class Point Class Point with thispublic class Point {

    public int x = 0;

    public int y = 0;

    //constructorpublic Point(int x, int y){ x = x; y = y;

    }public static void main(String args[]){

    Point p=new Point(10,20);

    System.out.println(p.x,p.y);

    }

    public class Point {

    public int x = 0;

    public int y = 0;

    //constructorpublic Point(int x, int y){

    this.x = x;

    this.y = y;}

    }

    Ambiguity is created between class

    instance x and parameter x (which x

    should be assigned which value)

    Here this.x refers to class

    instance x and it is assigned

    the value of parameter x

    Eg2)

  • 7/25/2019 this Keywordnew - Copy.pdf

    2/6

    public Loan(String type, double interest){

    this.type = type; this.interest =

    interest;

    }

    2) th iscan be used to invoked current class constructor.

    From within a constructor, you can also use the thiskeyword to call another constructor

    in the same class. Doing so is called an explicit constructor invocation Eg 1)

    class Loan{ private

    double interest;

    private String type;

    public Loan(){

    this(personal loan);

    }

    public Loan(String type){

    this.type = type;

    this.interest = 0.0;

    }

    }

    Eg 2)

    class Square

    { int

    height; int

    width;

    Square()

    {

    this(0,0);

    }

    Square(int side)

    {

    this(side, side);

    }

    Square(int height, int width){

  • 7/25/2019 this Keywordnew - Copy.pdf

    3/6

    this.height = height;

    this.width = width;

    }

    }

    class ImplSquare

    {

    public static void main(String args[])

    {

    Square sObj = new Square(4,6);

    System.out.println("Variable values of object : ");

    System.out.println("Object height = " + sObj.height);

    System.out.println("Object width = " + sObj.width);

    }}

    Output

    Variable values of object :

    Object height = 4

    Object width = 6

    In the above constructor this is used in a constructor to invoke the constructor of same class

    in case of constructor overloading. We can do this by using this keyword followed by the list

    of parameters, it is similar to invoking a method but only from within a constructor and

    whenever it is used, it has to be the first statement in the constructor.

    3)The this keyword can be used to invoke current class method

    (implicitly).

    You may invoke the method of the current class by using the thiskeyword. If you don't

    use the thiskeyword, compiler automatically adds this keyword while invoking the

    method.

    class CheckInvocation{

    void method1(){

    System.out.println("method is invoked");

    }

    void method2(){ this.method1(); //no need becausecompiler does it for you.

  • 7/25/2019 this Keywordnew - Copy.pdf

    4/6

    }

    void method3(){

    method2(); //complier will add this to invoke n() method as this.n()

    }

    public static void main(String args[]){

    CheckInvocation s1 = new CheckInvocation ();

    s1.method3(); //method3 calls method2 and it invoked and calls method1

    }

    }

    4) this keyword can be passed as an argument in the method.

    The thiskeyword can also be passed as an argument in the method. It is mainly used in

    the event handling.

    class CheckInvocation{

    voidmethod1(CheckInvocatin

    obj){

    System.out.println("method is invoked");

    }

    void method2(){

    method1(this);

    }

    void method3(){

    method2();

    }

    public static void main(String args[]){

    CheckInvocation s1 = new CheckInvocation ();s1.method3(); //method3 calls method2 and it invoked and calls method1

    }

    }

    5) The this keyword can be passed as argument in the constructorcall.

    We can pass the thiskeyword in the constructor also. It is useful if we have to use one object in

    multiple classes

    For Eg.

    class B{

    A obj;B(A obj){

  • 7/25/2019 this Keywordnew - Copy.pdf

    5/6

    this.obj=obj;

    }

    void display(){

    System.out.println(obj.data);//using data member of A class

    }

    }

    class A{ int

    data=10;

    A(){

    B b=new B(this);

    b.display();

    }

    public static void main(String args[]){

    A a=new A();

    }}

    Output:10

    6) The th iskeyword can be used to return current class instance.

    We can return the this keyword as an statement from the method. In such case, return type of

    the method must be the class type (non-primitive): Syntax of this that can be returned as a

    statement

    return_type method_name(){

    return this;

    }

    Eg,

    class A{

    A getA(){

    return this;

    }

    void msg(){

    System.out.println("Hello java");

    }

    }

    class Test{ public static void main(String

    args[]){ new A().getA().msg();

    }

    }

    Output:Hello java

  • 7/25/2019 this Keywordnew - Copy.pdf

    6/6

    Where Not to use this Keyword.

    1)thisis a final variable in Java and you can not assign value to this. thiswill result in

    compilation error: this= new Loan(); //cannot assign value to final variable : this

    2)thiskeyword can not be used in static context

    i.e. inside static methods or static initializer block. if use this inside static

    context you will get compilation error as shown below

    public static void main(String args){

    this.toString{}; //compilation error: non static variable thiscan not be used in static context

    }