public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout myLayout=new RelativeLayout(this);
Button myButton=new Button(this);
myLayout.addView(myButton);
setContentView(myLayout);
}
Asked
Active
Viewed 33 times
-3

rghome
- 8,529
- 8
- 43
- 62

Harshal Patil
- 1
- 1
-
could you explain more your issue? And please update the title to provide more information. In Java 'this' refers to the class instance. – Mario Santini Dec 13 '17 at 18:34
-
So does it mean that in my above case it refers to the object of class MainActivity – Harshal Patil Dec 13 '17 at 18:46
1 Answers
0
this refers to the object which you are calling the method. When you will create an object, suppose obj and then call obj.onCreate (), this will be a reference to obj.

Shiksha Verma
- 130
- 9
-
I get that it refers to the obj. But it looks complex for me in above case.In my question, 'this' refers to MainActivity class ? – Harshal Patil Dec 13 '17 at 18:44
-
It refers to object and not class. And that object will be an instance of MainActivity – Shiksha Verma Dec 13 '17 at 18:45