Results 1 to 4 of 4
  1. #1
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default Concept question

    Please forgive my ignorance, I am trying to basically learn all of this on my own.

    Working on my next weeks assignment, I did some googling to figure out how to create a constructor when building another class.

    I found some code snippets so that I can see what is going on, but one part I am having trouble understanding the reasoning behind.

    The "this." statement. It seems that when declaring the variables in the code snippet, the programmer used that on part of the variables, but not others. At first I thought it was needed when using a constructor, which makes sense, but since the other variable is just a normal declaration, I am losing sight of why this. is needed.

    I googled it, and I am still having trouble understanding the reasoning behind it. So if anyone can put it into dumber than dirt laymen terms, I would be appreciative :).

    Here is the code snippet: (not my code, credit to hardwired on these forums)

    Java Code:
    class Employee2
    {
        String empName;
        double hoursWorked;
        double payRate;
        double total;
        
        public Employee2(String name, double hours, double rate)
        {
            this.empName = name;
            this.hoursWorked = hours;
            payRate = rate;
    I understand that empName is a string variable, that goes into a field that will be used for name, but why use this.empName = name; rather than empName=name;

    And why isnt payRate listed as this.payRate=rate; if it because of the constructor.

    Once again, apologies for the basic question, Im just trying to learn how the coding works, rather than just learning the code.

    Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    afaik, the main time that you have to use this.myVariable is when the field name and the parameter name are the same, otherwise the compiler will not know when you mean to use the field and not the parameter and will default to the parameter since it is the most local identifier of the two.

    i.e.,

    Java Code:
    private int foo;
    private int baz;
    
    public void setFoo(int foo) {
       this.foo = foo; // this is needed since parameter and field name are the same
    }
    
    public void setBaz(int myBaz) {
       baz = myBaz;  // this is not needed since they are different.
    }

    There are other times you'll need to use this, such as when you need to pass a reference of the current object into something such as into a method.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As fubar said already, it really just allows you to not need to think of a new variable name in constructors and setters.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. using menu concept
    By kusuma in forum New To Java
    Replies: 4
    Last Post: 02-02-2011, 06:47 AM
  2. Need help in thread concept
    By umapathy_sekar in forum Advanced Java
    Replies: 2
    Last Post: 11-11-2010, 07:25 AM
  3. enter key concept......
    By silversurfer2in in forum AWT / Swing
    Replies: 5
    Last Post: 06-29-2010, 12:36 PM
  4. Map concept in JAVA
    By Mathan in forum New To Java
    Replies: 19
    Last Post: 06-27-2008, 07:02 AM
  5. mail concept
    By indirani in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 02:30 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
  •