0

I was using static makeText() function of Toast class in android studio.Now,as makeText() function requires an object of Context class ,what we can do is create an object of Context class by below given code.

Context context = getApplicationContext();

and use this context object as the parameter in makeText() function.That's OK.

But I was reading a book which used the makeText() function along with show() as below given code.

Toast.makeText(this,"Can you see me",Toast.LENGTH_SHORT).show();

I read on this page that this keyword can be used to get a Context.

So,my question is why using this keyword as a parameter of a static function of Toast class refers/gives/returns specifically a Context when MyActivity(from below code) class extends directly/indirectly AppcompatActivity,FragmentActivity,Activity,ContextThemeWrapper,ContextWrapper classes or why not 'this' keyword refers to an object of immediate class 'MyActivity' ?

public class MyActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);

    Toast.makeText(this,"Bhai app chalu ho gai",Toast.LENGTH_SHORT).show();

    Log.i("info","Done creating the app");
}

}

Community
  • 1
  • 1
Kartik Watwani
  • 129
  • 1
  • 1
  • 7
  • 1
    You should probably read some books about **basic** Java first. Seriously: android adds a whole lot layer of complexity; and you are definitely not ready for that when you don't understand what **this** is about. – GhostCat Jan 18 '17 at 07:30
  • 1
    Because `Activity` extends `Context` [Doc](https://developer.android.com/reference/android/content/ContextWrapper.html) – Vüsal Jan 18 '17 at 07:33
  • 1
    @GhostCat This question is not a duplicate of the one it has been closed for. The OP is specifically asking about how `this` in `AppCompatActivity` can be passed to a method that takes `Context` parameter. This question is more about polymorphic substitutions and not about `this`. Please do spend some time understanding the semantics of the question before closing it. I urge you to open this question or find a more meaningful duplicate of it. – Chetan Kinger Jan 18 '17 at 07:34
  • 1
    @Kartik `this` in `AppCompatActivity` refers to the current instance which is of type `AppCompatActivity`. Also, `AppCompatActivity` is a subclass of `Context` and can therefore be passed to a method that takes a `Context` as a parameter. This is one of the ways in which `Polymorphism` is used in the real world. – Chetan Kinger Jan 18 '17 at 07:39
  • 3
    @CKing He writes: *I read on this page that this keyword can be used to get a Context.* ... I think he doesn't understand in the first place what *this* is about. I get your point; but I think duplicating to some polymorphism-explaining other question could as well be misleading. I dont mind when enough people come together to over-rule my close vote; but for now I will keep it as it is. – GhostCat Jan 18 '17 at 07:46
  • @CKing Does using `this` keyword creates more memory overheads than directly creating a `Context` object and using it as a parameter, because using an object of class which extends 5-6 classes directly/indirectly will have many functions and variables to create? ,also I think when you said that `this` refers to `AppCompatActivity` ,I feel that it refers to `MyActivity` since it is more immediate class. – Kartik Watwani Jan 18 '17 at 07:56
  • 3
    `this` means `myself`. When you say `myself`, you are not only `Kartik Watwani` StackOverflow user, but a human, a form of life as well. Same way Activity is a Context as well as Activity because it extends Context. – Vladyslav Matviienko Jan 18 '17 at 08:00
  • @GhostCat Fair enough and point taken. – Chetan Kinger Jan 18 '17 at 08:14
  • 1
    @KartikWatwani Again: please read the question I duplicated to. Using *this* doesn't create an object at all. It is just a **reference** to an object that **already** exists. And: creating objects doesn't create "methods". An object is nothing more but some memory for the fields that belong to each object. The methods are part of the class loaded by the JVM; and there are tables will guide which method belongs where. So: again: you better study a lot of the java basics. First. Now. – GhostCat Jan 18 '17 at 08:24

0 Answers0