1

How does this android Code work - "this" is being referenced in a static way(by the class name) in makeText method. ( 1st Parameter)

public class QuizActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        private Button mTrueButton;
        mTrueButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                Toast.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show();
                }
        });
    }

}

The question is about how does "qualified this" work and not why does it exist or its purpose, but about how is "classname.this" being internally resolved to the enclosing object reference?Classname.this is usually used and works for static members , it shouldn't conceptually work for member variables like "this"; unless made to work that way

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
nikel
  • 3,402
  • 11
  • 45
  • 71
  • 5
    You have an anonymous inner class. Read up on that. – Sotirios Delimanolis Jan 17 '14 at 15:38
  • I know that but how is "this" is being used in a static way (QuizActivity.this)? – nikel Jan 17 '14 at 15:39
  • `QuizActivity.this` refers to activity context. http://developer.android.com/reference/android/content/Context.html – Raghunandan Jan 17 '14 at 15:40
  • 4
    This question appears to be off-topic because it is about basic java concepts – njzk2 Jan 17 '14 at 15:40
  • http://stackoverflow.com/questions/2731719/how-can-this-of-the-outer-class-be-accessed-from-an-inner-class – Sotirios Delimanolis Jan 17 '14 at 15:40
  • The question is about how is this which pertains to Objects , is being accessed by class definition, i.e in a static way? – nikel Jan 17 '14 at 15:43
  • See the link I've posted. – Sotirios Delimanolis Jan 17 '14 at 15:46
  • I saw that. The question asks how does that work?Technically? – nikel Jan 17 '14 at 15:48
  • That's just the syntax. There's no `static` reference. – Sotirios Delimanolis Jan 17 '14 at 15:48
  • just `this` refers to the anonymous subclass of `View.OnClickListener` you've created inside an instance of `QuizActivity`. The method called there wants a reference to the outter `QuizActivity` instance (not the static class) so just `this` would refer to the wrong thing. And the syntax `NameOfOutterClass.this` is just how it is defined. – zapl Jan 17 '14 at 15:58
  • Isn't stackOverflow a place for asking questions like how Java Works internally? – nikel Jan 24 '14 at 06:11
  • This question is NOT about how "qualified this" is used Or what's the purpose of its existence, but about how is "classname.this" being internally resolved to the enclosing object reference?i.e , usually classname.this should mean something which is being retrieved out of the class definition and hence , it shouldn't conceptually work for objects ; unless made to work that way – nikel Jan 24 '14 at 06:13
  • By caps , I just wanted to emphasize some keywords , because they got missed earlier.Unfortunately , there's no bold formatting for comments(afaik). Was just emphasizing my humble opinion. – nikel Jan 24 '14 at 09:18
  • **Bold** text is achieved by putting 2 asterisks on either side of the text. *Single* asterisks gives italics. – takendarkk Dec 28 '14 at 06:02

1 Answers1

0

That is the standard way to access, from a private class, the mandatory object of the enclosing class that has to be related to your inner class instance.

Just think that, if an inner class is not static, you don't just instantiate objects of the inner class; you associate if with an instance of the enclosing class as described in

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Jorge_B
  • 9,712
  • 2
  • 17
  • 22