Results 1 to 7 of 7

Thread: this keyword

  1. #1
    crazy is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default this keyword

    can anyone tell me how this keyword work in Java.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: this keyword

    What does your research tell you?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

  4. #4
    crazy is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: this keyword

    i know that "this" is a reference to the current object but i want to know that how it works!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: this keyword

    Quote Originally Posted by crazy View Post
    i know that "this" is a reference to the current object but i want to know that how it works!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    The compiler generates a reference to the current object when it sees 'this'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: this keyword

    Quote Originally Posted by crazy View Post
    i know that "this" is a reference to the current object but i want to know that how it works!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Why? Do you also need an explanation about the word "myself" ? No, you just assume that it refers to the person that says it. You should do the same with the word 'this' and make your life easier.

  7. #7
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: this keyword

    some really good examples of "this"

    example 1 when you want in constructor to give values in variables like that :

    Java Code:
    class MyClass{
    	private String name;
    	private int numberId;
    	public MyClass(String name ,int numberId){
    		this.name = name ;
    		this.numberId = numberId;
    	}
    }
    because without "this" we would have problem ;)

    another very useful use for "this" is when you have two class and you want to send an object in the other class but let me give you an example again ..
    Java Code:
    class MyClass{
    	private String name;
    	private int numberId;
    	
    	public MyClass(String name ,int numberId){
    		this.name = name ;
    		this.numberId = numberId;
    	}
    	
    	public void checkElements(SecondClass element){
    		....                                                       // do something ...
    	}
    }
    
    class SecondClass{
    	private MyClass object1 ;            // keeps one MyClass element
    	
    	public SecondClass(MyClass object1){
    		this.object1 = object1 ;
    	}
    	
    	public void checkObjects(){
    		object1.checkElements(this);    // class checkElements() method in myClass class and with this object (SecondClass object) as parameter
    	}
    }
    
    class MyMainClass{
    	public static void main(String args[]){
    		MyClass element = new MyClass("Jordan",18451);
    		SecondClass element2 = new SecondClass(element);
    		element2.checkObjects();
    	}
    }
    i hope i help you ;)
    Last edited by ShadowWalker; 07-07-2013 at 05:23 PM.

Similar Threads

  1. new keyword?
    By MW130 in forum New To Java
    Replies: 9
    Last Post: 05-08-2013, 03:26 PM
  2. The this keyword
    By Army in forum New To Java
    Replies: 15
    Last Post: 12-14-2012, 04:01 AM
  3. Use of keyword instanceof
    By darek9576 in forum New To Java
    Replies: 3
    Last Post: 03-14-2010, 10:35 PM
  4. this keyword
    By coltragon in forum New To Java
    Replies: 10
    Last Post: 03-01-2010, 09:20 AM
  5. Use of this keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:32 PM

Posting Permissions

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