2

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

khelwood
  • 52,115
  • 13
  • 74
  • 94
Avinash Kumar
  • 258
  • 3
  • 20

3 Answers3

13

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
  • 669,072
  • 92
  • 771
  • 1,162
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
  • 303
  • 1
  • 12
2

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
  • 510
  • 4
  • 17