This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: this
It's not a secret anymore!