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 6 of 6

Thread: this. Keyword

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    South Africa Johannesburg
    Posts
    4
    My Mood
    Mellow
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default this. Keyword

    Hi Jnic here
    Please can anybody give me some clarity on the various purposes of the this Keyword?

    public class Employee {
     
      private String name;
      private MyDate birthDate;
      private float  salary;
     
      // Constructor
      public Employee(String name, MyDate DoB, float salary) {
        this.name = name;
        this.birthDate = DoB;
        this.salary = salary;
      }
     
      public String getDetails() {
        return "Name: " + name + "\nSalary: " + salary
               + "\nBirth Date: " + birthDate;
      }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: this. Keyword

    public class Employee {
     
      private String name; //This variable is an instance or class variable
      private MyDate birthDate;
      private float  salary;
     
      // Constructor
      public Employee(String name, MyDate DoB, float salary) {
                           //The variable name above here is a variable local to the constructor
                //this.name refers to the class variable and name refers to the local variable
        this.name = name;
        this.birthDate = DoB;
        this.salary = salary;
      }
     
      public String getDetails() {
        return "Name: " + name + "\nSalary: " + salary
               + "\nBirth Date: " + birthDate;
      }
    }

    this always refers back to the object itself.
    Make two instances of any Object, using the example above:
    Employee A = new Employee(...);
    Employee B = new Employee(...);
    During construction of object A, this.name refers to the instance variable name of object A, and refers to the instance variable name of object B during construction of B.

  3. The Following User Says Thank You to jps For This Useful Post:

    Jnic (May 30th, 2013)

  4. #3
    Junior Member
    Join Date
    May 2013
    Location
    South Africa Johannesburg
    Posts
    4
    My Mood
    Mellow
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Talking Re: this. Keyword

    Thanks jps
    So the this is used to resolve ambiguity between instance Variables of the OBJECT(S) and local variables of the constructor should they be of the same name

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: this. Keyword

    They are commonly the same name because they do really represent the same thing. One of those grey areas, it depends on who you ask.
    Some always use different names, some always write out this. for every use, some use this. only when required.

    I lean toward the use of this. and the same variable name because I like the variable name to fully inform the reader what information is held there regardless of whether the data is on the way in, way out, or being stored.
    Much of the old code I read, programmers who went with different names used the better name for the class variable, and left the method variable name of poor quality. Now everyone who calls the method has to decide what that horrible variable name really stands for.
    So I would rather see the same name myself, but again just my opinion

  6. The Following 2 Users Say Thank You to jps For This Useful Post:

    aussiemcgr (May 30th, 2013), Jnic (May 31st, 2013)

  7. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: this. Keyword

    I prefer to use this. whenever making a reference to a class variable. It is very helpful when you have a block of code with a bunch of different variables and statements to see this. and know, without a doubt, that the variable is a class variable and not a local variable.

    It's the little things in language syntax (both coding and otherwise) that make it easier to interpret.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Jnic (May 31st, 2013)

  9. #6
    Junior Member
    Join Date
    May 2013
    Location
    South Africa Johannesburg
    Posts
    4
    My Mood
    Mellow
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: this. Keyword

    jps
    Thank you for the clarification , So basically it is up to the discretion of the programmer to decide the intention

Similar Threads

  1. Use of final keyword
    By vilkas in forum Exceptions
    Replies: 5
    Last Post: December 17th, 2012, 03:17 AM
  2. Uses of throws keyword
    By vilkas in forum Exceptions
    Replies: 1
    Last Post: December 11th, 2012, 03:17 AM
  3. [SOLVED] The 'new' Keyword
    By TheSlowLearner in forum Object Oriented Programming
    Replies: 17
    Last Post: August 7th, 2012, 12:05 AM
  4. Super Keyword
    By shruthi in forum Java Theory & Questions
    Replies: 12
    Last Post: October 13th, 2011, 03:31 PM
  5. keyword Extends
    By chronoz13 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 27th, 2009, 07:30 AM