Results 1 to 4 of 4
Thread: 'this' question
- 05-12-2012, 05:04 AM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
'this' question
public class examprep {
int x = 0;
public examprep(int x){
this.x = this.x +1;
x = this.x +2;
}
public static void main(String[] args){
examprep a = new examprep(3);
System.out.println(a.x);
}
}
Hi everyone, firstly I was wondering what does a.x mean (in main method), also why does it print 1.
this.x = this.x + 1
so this.x = 1
then x = this.x + 2
so x = 3 doesn't it?
What am I doing wrong here? Thanks for your time
- 05-12-2012, 05:39 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: 'this' question
There are two x's. (A) one belongs to the class and (B) one is an argument to the constructor.
(A) In any constructor or non static method the instance variable can be referred to as "this.x". Anywhere else it be referred to as "a.x" where a is the instance whose corresponding x is meant. In non static methods we can also refer to it as "x" but only if there is no local variable with that name.Java Code:public class ExamPrep { int x = 0; // <------ (A) an instance variable is declared public ExamPrep(int x) { // <------ (B) a local variable is declared this.x = this.x +1; x = this.x +2; } public static void main(String[] args){ ExamPrep a = new ExamPrep(3); System.out.println(a.x); } }
(B) The local variable x is referred to using "x" but only within the constructor where it is declared. Outside of that constructor this variable cannot be referred to at all.
-----
[Edit] Also, please post readable code. The briefest examples should use standard Java coding conventions with respect to case. Use the "code" tags and make proper use of horizontal and vertical spacing.
- 05-12-2012, 06:01 AM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 142
- Rep Power
- 0
- 05-12-2012, 06:59 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: 'this' question
Basically you can define a method as a static method when it doesn't operate or access any non-static member of the class, such as variables and methods. When you tried to access them the compiler will complain.
Static methods usually get their data from parameters passes to them and work with these parameters to do something. A good candidate for static method is utility methods that do some calculation, for example. You can look at the java.lang.Math where you can see a lot of static method defined.Website: Learn Java by Examples
Similar Threads
-
Java Question [Beginner Question]
By joker760 in forum New To JavaReplies: 3Last Post: 12-13-2011, 04:01 PM -
question posted by indissa: library question.
By Fubarable in forum New To JavaReplies: 2Last Post: 11-18-2011, 01:14 AM -
Question concerning question marks and colons
By jim01 in forum New To JavaReplies: 17Last Post: 01-14-2011, 12:05 AM -
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks