Results 1 to 8 of 8
- 09-22-2011, 01:08 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 10
- Rep Power
- 0
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!
- 09-22-2011, 01:53 AM #2
Re: Totally beginer question —— what is "this"?
Here is Oracle's explanation Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
- 09-22-2011, 09:56 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 09-22-2011, 11:13 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 7
- Rep Power
- 0
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
- 09-22-2011, 11:24 AM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
Each of these lines speaks for itself; the comment is unnecessary.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 classLast edited by Iron Lion; 09-22-2011 at 11:27 AM.
- 10-02-2011, 06:18 AM #6
Member
- Join Date
- Sep 2011
- Posts
- 10
- Rep Power
- 0
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.
- 10-02-2011, 07:41 AM #7
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;
- 10-02-2011, 09:14 AM #8
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
Re: Totally beginer question —— what is "this"?
this:
‘this’ is an implicitly restricted pointer variable.
Every instance method of the class will have it.
‘this’ is used to hold the address of the object who is invoking the corresponding method.
‘this’ is recommended whenever both local and instance variables are same, in order to differentiate between them make use of ‘this’ for instance fields.
‘this’ is not supported in static methods.
for more queries and examples visit this page: Overloading and use of ‘this’ – Part7 | ExceptionHandle.com
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks