Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Why does my lecturer use the key word ".this"

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Why does my lecturer use the key word ".this"

    I've never actually understood what they key word ".this" is in Java, my lecturer seems to use it in every single place in his code. And when I asked him, he just said "I don't really know myself"
    He uses it in every single method, every single constructor, every god damn place.


    Why do you use it? Can somebody actually tell me?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why does my lecturer use the key word ".this"

    How is the keyword being used?

    One way you can use the this keyword is to pass a reference to yourself to another method. Something like:

    someMethod(this);

    The other use of the this keyword is to simply call methods of the same class. This is a bit extraneous, since the this keyword is implied. Something like

    this.someMethod();

    The one reason I've seen people use the second use of the this keyword is when using an IDE: typing "this." in most IDEs will bring up a list of the methods in that class. So it's just a lazy way to remember the names of methods you might want to use.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Why does my lecturer use the key word ".this"

    Look at these 2 pieces of code:
    public class Example {
     
    	private int x;
     
    	public Example(int x) {
    		this.x = x;
    	}
    }
    public class Example {
     
    	private int x;
     
    	public Example(int x) {
    		x = x;
    	}
    }
    They are not the same. In the first example a newly constructed object of type Example would have its attribute 'x' initialized with the given argument.
    The second example does absolutely nothing in its constructor since it assigns the value of the argument x to the argument x itself. Its a no-op.

    In these rare cases the "this" keyword is not only useful but absolutely necessary.
    If both the attribute and the parameter would have different names though the "this" keyword would not be necessary and could be omitted.

    Always typing "this" in front of attribute names is a safe way to make sure you dont ever run into copy-and-paste bugs when attribute and parameter names collide.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why does my lecturer use the key word ".this"

    Doh, can't believe I forgot the most common usage of the this keyword. Nice catch Cornix.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Word count, coutns "." as a word
    By Lashickk in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2013, 04:09 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 2
    Last Post: July 15th, 2012, 09:19 AM
  4. JButton with "Enter Key" keyboard action
    By chronoz13 in forum Java Swing Tutorials
    Replies: 2
    Last Post: March 14th, 2012, 06:49 AM
  5. JButton with "Enter Key" keyboard action
    By chronoz13 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: January 26th, 2010, 10:53 AM