File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "this" Watch "this" New topic


neelam samnani

Joined: Mar 12, 2001
Posts: 8
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");
int temp =;
if (temp > 5) {
Pls. help me.

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.

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:
subject: this
It's not a secret anymore!