Java This

29/08/2008

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 posted on 29/08/2008.
Ads by Google

70 comments on “Java This

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

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

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

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

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

  6. gud and short explaination includes all types of using this keyword

  7. Pingback: Java Double Brace Initialization

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

  9. thanx for explain this keyword………..

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

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

  12. when we use final keyword, explain with example

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

  14. thanks for the explanation now i am very clear about this

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

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

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

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

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

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

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

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

    Thanks this was new to me

  23. what is correctly mean my return this in a method

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

  25. i like ur explaination
    pls explain in detail

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

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

  28. 5th point is not clear…sir pls explain it

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

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

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

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

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

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

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

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

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

  38. need more and clear idea with good example

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

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>