This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
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 Elasticsearch in Action this week in the Big Data 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