• Post Reply Bookmark Topic Watch Topic
  • New Topic

is "this" a static variable ?  RSS feed

 
Preet Dholabhai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i try to use this from my main it will say "this" is not a static variable.

when i have a inner class and want the reference of the outer class, i use OuterClassName.this to get the reference.

Is this scenario an exception ? is my understanding correct ?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's not a static variable. That's just a special syntax that allows you to get a reference to the enclosing class.

If it was a static variable, it would have the same value regardless of which object you were in, but it doesn't. It refers to the specific instance of OuterClassName that contains the current object.
 
Naishadh Parmar
Ranch Hand
Posts: 95
C++ Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead use an object like this in your class

OtherClass instance = new OtherClass();
you can use the instance variable
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!