4

I know this refers to the current object. I am little confused about this.ClassName and ClassName.this when creating Intent.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Avinash Kumar
  • 288
  • 6
  • 21

3 Answers3

17

I am little confused about this.ClassName and ClassName.this when creating Intent.

The Classname.this syntax is used to refer to an outer class instance when you are using nested classes; see Using "this" with class name for more details.

However this.Classname is a compilation error ... unless you have declared an instance (or static) field with the name Classname. (That would be a daft thing to do, as well as being an egregious style violation.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3
Intent intent=new Intent(context, AcitivityName.class);

The first argument is just the context so when you write for ex. Main.this you are just referring to the context of that activity.

The second argument is the a Activity you want to start or whatever...

Obsthändler
  • 313
  • 1
  • 12
3

I'm assuming ClassName is just filling in for the current class's name. ClassName.this is exactly the same as simply writing this. So if a class had a variable called foo you could reference it from that class using ClassName.this.foo the same way you could with this.foo.

this.ClassName is not valid java as far as I know.

Alex Collins
  • 560
  • 5
  • 18