Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Iron Lion

Thread: Totally beginer question 覧 what is "this"?

  1. #1
    Callofdudey is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Totally beginer question 覧 what is "this"?

    The following section of codes are cut out of a more complete codes. To help illustrating where my question lies, I have written down side-notes every line.




    public class Calendar { //Defining a class called "Calendar"
    private int day; // defined a integer variable called "day" which can only be used within the Calendar class
    private int month; //defined a integer variable called "month" which can only be used within the Calendar class
    private int year; //defined a integer variable called "year" which can only be used within the Calendar class
    public Calendar(int day,int month,int year){ //Defining a method called "Calendar", having the same name as the class it is in, does having the same name make it "special"?
    this.day = day; // this line, and the two lines follows got me lost, what is "this"? What does "this.day" mean? In C, if you want to use some variable, you can just use it, but in java, looks like you have to put everything in a method, and cite them again?
    this.month = month;
    this.year = year;
    }


    public Calendar(Calendar date) { //defining a method outside the "Calendar", but also have the same name? Does it conflict with the method that is inside the "Calendar" class?
    this.day = date.day; //what's the relation of this "date" with the one inside the brackets after the method?
    this.month = date.month;
    this.year = date.year;
    }

    So my questions, besides ones that I mentioned in the annotation (highlighted with red), are:

    If there are serveral class in only one file, then to compile in correctly, the source code filename will have to be the same name as which class within?

    I've seen alot stuff like this:(keyword/variable/sensitive names).(keyword/variable/sensitive names), like "astring.length", can anyone tell me what are they? What does the magical dot, "." tell java compiler?


    Thanks in advance for your help!

  2. #2
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default Re: Totally beginer question 覧 what is "this"?


  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Totally beginer question 覧 what is "this"?

    You probably ought to look at the Oracle Java tutorials (the link in Zman's post is to one of them).
    Here's the one on constructors.
    If you need to ask what the dot is you possibly need to go right to the beginning though.

  4. #4
    Anoop is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Re: Totally beginer question 覧 what is "this"?

    "this" keyword is represents the current object reference in java.. It is actually the part of super which contains reference to the local object. With this u can specify the compiler the local class's reference.

    thanx
    Anoop

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Totally beginer question 覧 what is "this"?

    The reason for using this.day is that day is the identifier given to both the instance variable and the constructor argument. When multiple variables share the same identifier, Java looks at the most local scope first, so any variables outside that scope which share its name must be qualified somehow. Therefore, this.day = day sets the instance variable to the value held by the argument, whereas day = day would be thoroughly redundant and have no effect on the instance variable.

    If the argument was called something else (say d), then there would be no need to use this, since you could unambiguously say day = d.

    Edit: Also, you're overcommenting your code.
    Java Code:
    public class Calendar { //Defining a class called "Calendar"
    private int day; // defined a integer variable called "day" which can only be used within the Calendar class
    private int month; //defined a integer variable called "month" which can only be used within the Calendar class
    private int year; //defined a integer variable called "year" which can only be used within the Calendar class
    Each of these lines speaks for itself; the comment is unnecessary.
    Last edited by Iron Lion; 09-22-2011 at 11:27 AM.
    Callofdudey likes this.

  6. #6
    Callofdudey is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Totally beginer question 覧 what is "this"?

    Hey thanks yall, esp. Iron Lion, I read a little more and can see your point even clearer.

  7. #7
    Join Date
    Oct 2011
    Location
    HELL
    Posts
    8
    Rep Power
    0

    Default Re: Totally beginer question 覧 what is "this"?

    this refers to what current you use.

    if you're declaring an instance variable of an object, it implicitly include this. ex: this.variableA;

  8. #8
    arnold741 is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default Re: Totally beginer question 覧 what is "this"?

    this:

    奏his is an implicitly restricted pointer variable.
    Every instance method of the class will have it.
    奏his is used to hold the address of the object who is invoking the corresponding method.
    奏his is recommended whenever both local and instance variables are same, in order to differentiate between them make use of 奏his for instance fields.
    奏his is not supported in static methods.

    for more queries and examples visit this page: Overloading and use of ‘this’ – Part7 | ExceptionHandle.com

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  3. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •