Results 1 to 11 of 11

Thread: this pointer

  1. #1
    janani@tcs is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default this pointer

    Any example like without using 'this' pointer we cant have a solution?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: this pointer

    Java Code:
    class Foo{
    	private int bar;
    	public Foo(int bar){
    		this.bar = bar;
    	}
    }
    bar = bar would have no effect

    /edit: Advanced java?
    Last edited by eRaaaa; 08-21-2012 at 09:16 PM.

  3. #3
    janani@tcs is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: this pointer

    but i can write like

    class Foo{

    public Foo(int a){
    bar = a;
    }
    }
    without using this

  4. #4
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: this pointer

    This refers to the current object. Look into shadow variables, as well as local and instance variables.

  5. #5
    janani@tcs is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: this pointer

    @zyril ,can you please tell me, can I have a problem like without This i cant have a solution?

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: this pointer

    Quote Originally Posted by janani@tcs View Post
    @zyril ,can you please tell me, can I have a problem like without This i cant have a solution?
    Are you asking if there could ever be a situation where you need to use the keyword 'this'?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: this pointer

    I think so. Which raises the question... why?

    Without much thought I'll hazard the guess that you never actually need "this" unless nested classes are involved when it's necessary to disambiguate types rather than methods or variables. (someone will doubtless correct me if I'm wrong).

    But the real question is why you would prefer some contrived, verbose, frown inducing, eyebrow raising alternative when "this" would do the job nicely.

  8. #8
    janani@tcs is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: this pointer

    Quote Originally Posted by awinston View Post
    Are you asking if there could ever be a situation where you need to use the keyword 'this'?
    yes..

  9. #9
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: this pointer

    Quote Originally Posted by janani@tcs View Post
    yes..
    I think pbrockway2 handled this question nicely. Did you see post #8?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: this pointer

    Quote Originally Posted by eRaaaa View Post
    /edit: Advanced java?
    Certainly not. janani@tcs, please confine beginner questions to the New to Java section.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: this pointer

    If #8 is not clear, ask.

    My point was that if using "this" is clear and straight forward, use it.

    (If you google "java nested class this" you will find pages discussing the cases where "this" has to be used. Again, ask if they are unclear.)

Similar Threads

  1. Null pointer exception
    By Domo230 in forum New To Java
    Replies: 4
    Last Post: 02-28-2011, 10:21 AM
  2. Null pointer exceptio
    By grifan526 in forum New To Java
    Replies: 0
    Last Post: 03-05-2010, 02:00 AM
  3. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 02:10 PM
  4. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-17-2009, 11:51 PM
  5. A variable pointer
    By tapies in forum New To Java
    Replies: 13
    Last Post: 09-12-2008, 10:44 AM

Posting Permissions

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