Java This

Last modified on August 1st, 2014 by Joe.

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?

Comments on "Java This"

  1. vinod says:

    Thanks… tat was quite clear… :)

  2. nandu says:

    tat was very clear.tnqs

  3. vikram says:

    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 says:

    thanks 4 d clear explanation

  5. archana chitta says:

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

  6. surya says:

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

  7. tanu says:

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

  8. As says:

    That was quite clear…thx

  9. murali says:

    tan q for given cler explanation

  10. Anamika says:

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

  11. Anonymous says:

    gud and short explaination includes all types of using this keyword

  12. […] Second brace is an initialization block. That too you have seen in it a class for initialization. You may have used a static initialization block. When you use the initialization block for an anonymous inner class it becomes java double brace initialization. The inner class created will have a reference to the enclosing outer class. That reference can be used using the ‘this’ pointer. […]

  13. niyaz says:

    I need more idea

  14. subbu says:

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

  15. mukesh says:

    thanx for explain this keyword………..

  16. mukesh says:

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

  17. bhoopendra kaurav says:

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

  18. Alluri Reddy says:

    when we use final keyword, explain with example

  19. Bala says:

    Very clear explanation. Thanks.

  20. Anonymous says:

    so bad

  21. SandeepBandari says:

    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.

  22. srekanth.kolla says:

    thanks for the explanation now i am very clear about this

  23. pravin says:

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

  24. chandeep says:

    all clear now.
    thanks

  25. sudokhina says:

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

  26. Anonymous says:

    thankkkss yyyaarrr

  27. sandip says:

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

  28. alex says:

    awesome dude!

  29. Palanisamy says:

    Nice Tutorial we need more..

  30. greenxgene says:

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

  31. ashish bajpai says:

    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

  32. ajai kumar gaur says:

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

  33. mahamed says:

    thanks alot

  34. mahamed says:

    thanks a lot master

  35. Rufatet says:

    thanks for clear info

  36. santosh kumar chourasiya says:

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

  37. Anonymous says:

    very well explanation…….

  38. Palanisamy says:

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

  39. Divya says:

    really helpful.. Thank you very much.

  40. Shazz says:

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

    Thanks this was new to me

  41. Anonymous says:

    what is correctly mean my return this in a method

  42. Teja says:

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

  43. sonali says:

    Thanks for your useful information…… :D

  44. Angel says:

    i like ur explaination
    pls explain in detail

  45. Anil says:

    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.

  46. vana says:

    thanks for clarifying in short

  47. ambru says:

    good sir ….

  48. madhu says:

    quite understanding

  49. masterk says:

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

  50. Jyotish says:

    5th point is not clear…sir pls explain it

  51. samir says:

    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..

  52. Anonymous says:

    this is used for return object

  53. Arjun chand says:

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

  54. ruchi says:

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

  55. geetu says:

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

  56. vinayak says:

    beautiful explanation

  57. AlegnaSamoht says:

    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.

  58. Zampos says:

    Thanx ,am now secured..

  59. avinash says:

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

  60. avinash says:

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

  61. rohit says:

    what is the meaning of current instanse

  62. rohit says:

    what is the meaning of current instance

  63. Amar Sannaik says:

    nicely said.

  64. Avik says:

    Thank you.It really helped.

  65. k.manjula says:

    Explain the use of this pointer with an example program?

  66. k.manjula says:

    what is the pointer in that above question?

  67. Pro_hacker says:

    // 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.

  68. Pro_hacker says:

    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 :*

  69. Sachindra says:

    need more and clear idea with good example

  70. Korny says:

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

  71. Anonymous says:

    Say as detailed

Comments are closed for "Java This".