Java This

Definition for java this keyword:

Java this keyword is used to refer the current instance of the method on which it is used.

Following are the ways to use java this

1) To specifically denote that the instance variable is used instead of static or local variable.That is,

private String javaFAQ;
void methodName(String javaFAQ) {
this.javaFAQ = javaFAQ;
}

Here this refers to the instance variable. Here the precedence is high for the local variable. Therefore the absence of the “this” denotes the local variable. If the local variable that is parameter’s name is not same as instance variable then irrespective of this is used or not it denotes the instance variable.

2) Java This is used to refer the constructors

public JavaQuestions(String javapapers) {
this(javapapers, true);
}

Java This invokes the constructor of the same java class which has two parameters.

3) Java This is used to pass the current java instance as parameter

obj.itIsMe(this);

4) Similar to the above, java this can also be used to return the current instance

CurrentClassName startMethod() {
return this;
}

Note: This may lead to undesired results while used in inner classes in the above two points. Since this will refer to the inner class and not the outer instance.

5) Java This can be used to get the handle of the current class

Class className = this.getClass(); // this methodology is preferable in java

Though this can be done by, Class className = ABC.class; // here ABC refers to the class name and you need to know that!

As always, java this is associated with its instance and this will not work in static methods.

How is(java) this?

This Core Java tutorial was added on 29/08/2008.

«

»

Comments on “Java This”

  1. vinod

    Thanks… tat was quite clear… :)

  2. nandu

    tat was very clear.tnqs

  3. vikram

    thanks for the article, but i am still not clear at point 3,4,5. could you give more explanation keyword “this” as parameter and return type.
    thanks

  4. archana chitta

    thanks 4 d clear explanation

  5. archana chitta

    can u giv a detailed expln’n on this with getClass()

  6. surya

    nice but….why did’t u working for the static members
    thank u for above descrcaption….overall gooood

  7. tanu

    it was awesome,. now i understand the this keyword.

  8. As

    That was quite clear…thx

  9. murali

    tan q for given cler explanation

  10. Anamika

    thnx for the explanation….somewhat got it……………

  11. Anonymous

    gud and short explaination includes all types of using this keyword

  12. niyaz

    I need more idea

  13. subbu

    Please explain me 3 4 5 point clearly and indepth..Please

  14. mukesh

    thanx for explain this keyword………..

  15. mukesh

    i m not understanding 5th points please explain it ,,,,,,,,,,,,,

  16. bhoopendra kaurav

    thanks for clear explan but pls give full discription over
    over point 3,4,5

  17. Alluri Reddy

    when we use final keyword, explain with example

  18. Bala

    Very clear explanation. Thanks.

  19. Anonymous

    so bad

  20. SandeepBandari

    I think most of the members are not getting with 2 or 3 lines of code. We need the total explanation with program having clarity output.

  21. srekanth.kolla

    thanks for the explanation now i am very clear about this

  22. pravin

    thanks alot. now i am very clear about this java keyword

  23. all clear now.
    thanks

  24. sudokhina

    plz explain 5th point with more eg..it is not so clear…it wil better if u give more eg..

  25. Anonymous

    thankkkss yyyaarrr

  26. sandip

    to detail use the in different class how to appy to more than classes.

  27. alex

    awesome dude!

  28. Palanisamy

    Nice Tutorial we need more..

  29. I was trying hard to understand this until I read this.

  30. ashish bajpai

    IM VERY MUCH PLEASED WITH THE GIVEN INFORMATION
    CAN U PLEASE GIVE ME OUTPUT OF THE FOLLOWING LINE

    System.out.println(this);

    i know ‘this’ is used to access refrence of the current object
    rent object

  31. ajai kumar gaur

    thanks a lot sir because you give me more knowledge about THIS in java.

  32. mahamed

    thanks alot

  33. mahamed

    thanks a lot master

  34. Rufatet

    thanks for clear info

  35. santosh kumar chourasiya

    very candid explanation of “this” keyword thanks for this.

  36. Anonymous

    very well explanation…….

  37. Palanisamy

    Got some idea about THIS keyword.. Thank you very much

  38. Divya

    really helpful.. Thank you very much.

  39. Shazz

    Java “this” is used to invoke the constructor of the same java class which has 2 parameter.

    Thanks this was new to me

  40. Anonymous

    what is correctly mean my return this in a method

  41. Teja

    Very nice.
    Can you please explain why “this” is used in
    “addkeyListener(this)” statement.What does it mean?

  42. Thanks for your useful information…… :D

  43. Angel

    i like ur explaination
    pls explain in detail

  44. Anil

    That about this in case of Inheritance
    like if one class extends second class

    class base
    {
    String name;
    base()
    {
    System.out.println(this.getClass()+”
    Name:-”+this.name);
    }
    base(String name)
    {
    this.name=name;
    System.out.println(this.getClass()+”

    Name:-”+this.name);
    }
    void show()
    {

    System.out.println(this.getClass()+”Name

    :Method”+this.name);
    }
    }
    class ThisExp extends base
    {
    String name;
    ThisExp(String name)
    {
    super(“Mohan”);
    this.name = name;

    System.out.println(this.getClass()+”Name

    :==”+this.name);
    }
    /* void show()
    {

    System.out.println(this.getClass()+”Name

    :Method”+this.name);
    }
    */

    public static void

    main(String s[])
    {
    base b= new ThisExp(“Amit”);
    b.show();
    }
    }
    Please Explain about this.

  45. thanks for clarifying in short

  46. ambru

    good sir ….

  47. madhu

    quite understanding

  48. masterk

    Is “this” is a local variable of any function or it is a non-static function.

  49. Jyotish

    5th point is not clear…sir pls explain it

  50. samir

    I want to research about “pointer handling in Java”.. i found that it can be done by using socket programming.So,any guidance about that give me….I want to make a research about that..

  51. Anonymous

    this is used for return object

  52. Arjun chand

    Simple and good explanation,Can you please give me detail description on your point no 3 & 4.I would be greatly obliged.

  53. ruchi

    hey…. u made things easy realy grt job.. thnx 4 it..ruchi

  54. geetu

    really very good explanation on “this” in java.. thank you s o much sir….

  55. vinayak

    beautiful explanation

  56. AlegnaSamoht

    Thank you very much and GOD bless u Sir for ur vivid explanations.

    Would be grateful if u put some codes especially for the 2,3,4&5 ways of ‘this’ pointer.

  57. Zampos

    Thanx ,am now secured..

  58. i want to ask one question why is must compulsory this() and super() is first statement y told the answers i m avinash sharma

  59. please any body told the answers for my questions why is must compluosry this ans super in first statement

  60. rohit

    what is the meaning of current instanse

  61. rohit

    what is the meaning of current instance

  62. Amar Sannaik

    nicely said.

  63. Avik

    Thank you.It really helped.

  64. Explain the use of this pointer with an example program?

  65. what is the pointer in that above question?

  66. Pro_hacker

    // Perfect Example to understand “this” keyword//

    import java.io.*;

    class MeanValueClass
    {

    int x,y;
    void getdata(int x,int y)
    {

    this.x=x;
    this.y=y;

    System.out.println(“In getdata x:”+x+”\ty:”+y);

    }

    int putdata()
    {
    System.out.println(“In putdata x:”+x+”\ty:”+y);
    int ans=((x+y)/2);
    return(ans);
    }
    };

    class MeanValue
    {
    public static void main(String args[])throws IOException
    {
    int a,b;
    int z=0;
    BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
    System.out.println(“Enter any two integer numbers to find mean value”);
    a=Integer.parseInt(br.readLine());
    b=Integer.parseInt(br.readLine());

    MeanValueClass m=new MeanValueClass();
    m.getdata(a,b);

    System.out.println(“Mean value : “+m.putdata());
    }
    }

    This code will surely illustrate the use of “this”

    if you are confusing then “just remove this from getdata()”
    you will see the difference.

  67. Pro_hacker

    In short using this keyword you are calling parents of variable/method/constructor

    without it you are calling their childrens i.e.Local variables :D XD :*

  68. Sachindra

    need more and clear idea with good example

  69. Korny

    What would be the difference if you used a class name instead of this?

Your Comment