aspose file tools*
The moose likes Mock Exam Errata and the fly likes this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "this" Watch "this" New topic
Author

this

neelam samnani
Greenhorn

Joined: Mar 12, 2001
Posts: 8
Hi,
I got in trouble with "this " , Why this program is not giving compilation error, that "this " is undefined variable.

public class Test {
static int total = 10;
public static void main (String args []) {
new Test();
}
public void Test () {
System.out.println("In test");
System.out.println(this);
int temp = this.total;
if (temp > 5) {
System.out.println(temp);
}}
}
Pls. help me.


Neelam
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi neelam,
Every non-static method inherently has a 'this' variable. Since your Test() method is non-static 'this' is defined.
Regards,
Manfred
Philosopher
Greenhorn

Joined: Mar 15, 2001
Posts: 6
Yeah actually 'this' variable points to the current object for which the method is being executed. For example if you create an object and call a non-static method. 'this' variable will be passed to the method by default and you will be able to use it to access the attributes/operations of the current object for which the method is being executed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this
 
Similar Threads
runtime error
Question on Thread Synchronization
Diff b/w "this" & "toString"
what does this mean here?
John Hunt M.E Q17