Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

is "this" a static variable ?  RSS feed

 
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 ?
 
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.
 
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!