Results 1 to 3 of 3
  1. #1
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    35
    Rep Power
    0

    Default Use of this operator

    What is difference between using this for object member and primitive member?

    Java Code:
    public classs KnightOfTheRoundTable {
       private String name;
       private HolyGrailQuest quest;
    
       public KnightOfTheRoundTable(String name) {
          this.name = name;
          quest = new HolyGrailQuest();
       }
    
       ...........
    }
    Can't we write this.quest = new HolyGrailQuest() in line 7?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Use of this operator

    Quote Originally Posted by anjibman View Post
    Can't we write this.quest = new HolyGrailQuest() in line 7?
    Sure you can but because there's no local variable or parameter with the name 'quest' in scope (that hides a member with the same name) it isn't needed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Use of this operator

    ya anything you call from within the class basically automatically has a this. before it. Its up to you if you want to include it or not.

Similar Threads

  1. Replies: 5
    Last Post: 01-21-2012, 11:02 PM
  2. Problem With * Operator
    By misant in forum New To Java
    Replies: 26
    Last Post: 01-18-2012, 06:26 AM
  3. The modulo operator
    By Blaedel in forum New To Java
    Replies: 2
    Last Post: 10-09-2011, 04:29 AM
  4. Java OR operator || help!
    By BeardedCamel in forum New To Java
    Replies: 2
    Last Post: 09-19-2011, 08:55 PM
  5. help with not equal to operator !=
    By manowar689 in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 12:10 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
  •