Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: keyword "this"

  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    8
    Thanked 0 Times in 0 Posts

    keyword "this"

    Just a quick question.

    Should we get used to using "this.variableName" or just use a different name?

    What would programmers with lots of experience commonly use?

    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Java uses variable masking for its member properties, so this. is not required unless you are naming properties the same as local variables or arguments (which I often do).
    I always use this. though, its a habit since I do so much PHP programming which does not have scope masking for its variables or methods.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Friend can you tell me? is it only for java?

  • #4
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. You can use this anywhere a reference to an object of the current class' type is permitted.

    Example:

    public class Class2{

    int number;
    public Class2(int number){
    this.number = number;
    }
    }

  • #5
    New Coder
    Join Date
    Aug 2014
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In java, this is a reference variable that refers to the current object.

    Usage of this keyword
    this keyword can be used to refer current class instance variable.
    this() can be used to invoke current class constructor.
    this keyword can be used to invoke current class method (implicitly)
    this can be passed as an argument in the method call.
    this can be passed as argument in the constructor call.
    this keyword can also be used to return the current class instance.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •