• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"this" keyword doubt

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

In the below code why "this.var" and "super.var" both printing the value in Base class? Please explain me whats happening here. What is "this" refers here?


class Base
{
int var=10;
}

class Outer
{
public void method()
{
class Inner extends Base
{
public void local_method()
{
int var=100;
System.out.println(var);
System.out.println(this.var);
System.out.println(super.var);
}
}
Inner i=new Inner();
i.local_method();
}
}

class Other
{
public static void main(String[] args)
{
new Outer().method();
}
}


Thanks in advance!

Regards,
Surekha.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the below code why "this.var" and "super.var" both printing the value in Base class? Please explain me whats happening here.


Inner inherits 'var' from the Base class.

What is "this" refers here?


It refers to the current object; the one whose method is being called.

The Java� Tutorial - Using the this Keyword
[ November 14, 2005: Message edited by: Steve Morrow ]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is an inner class in non-static context of class. Also extends .

Reference to this.var or .var from refers to var in (var declared in the is not an instance variable at all it is local variable so it cannot be referred to using this or super.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to try this, to know the diff b/w "this" and "super".

 
Surekha Reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for response Steve...

Regards,
Surekha.
 
Surekha Reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for your responses. Now i got it.

Regards,
Surekha.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai surekha ,i did not understand anyones explanation regarding your doubt
can u explain me what those gentlemen are trying to say?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this.var would be the value of the varibale var of the class.
Since the inner class extends the base calss the values of the variable var is same for
this.var as well as super.var.
The var defined in the method is not available outside the method and is not a variable of the Inner class therefore this.var does not refer to it.
 
Surekha Reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashok,

The variable 'var' is defined in two places. One in 'Base' class and the other in the method.

The simple name 'var' refers to local variable defined in the method.

'this.var' refres to instance variable in the current object which is nothing but Inner class object. ( Actually 'var' is not defined in Inner class but we are inheriting it from Base class).

'super.var' refres to instance variable in the Super class object.

So here both 'this.var' and 'super.var' printing the same value which is defined in Base class.

To know the difference between these two u can check the example given by Lakshmanan Arunachalam. It's a very good example.

Hope this is clear.

Thanks,
Surekha.
 
reply
    Bookmark Topic Watch Topic
  • New Topic