-1

Actually in Android, while using intent to move to a different activity using OnClick Listener object I use the snippet

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the content of the activity to use the activity_main.xml layout file
    setContentView(R.layout.activity_main);

    TextView numbers = findViewById(R.id.numbers);
    numbers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(**MainActivity.this**, NumbersActivity.class));
        }
    });


}

}

But even the snippet below works well

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the content of the activity to use the activity_main.xml layout file
    setContentView(R.layout.activity_main);

    TextView numbers = findViewById(R.id.numbers);
    numbers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(**new MainActivity()**, NumbersActivity.class));
        }
    });


}

}

To highlight the difference I would like to mark the difference in .... Does this mean that while trying to access the Outer Class of the Anonymous Inner class we will have to access it using the snippet "OuterClass.this" inside the Inner class . But when I use "new OuterClass()" instead of "OuterClass.this" inside the anonymous inner class even this gives a correct result. I want to ask whether both the formats are same or they have any hidden difference as a consequence.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Deep Roy
  • 89
  • 10
  • you cannot even compare this two situations ... java's basics `operator new` and `this` ... obviosuly `Intent` contructor expects to get any Context object as first parameters – Selvin Dec 01 '17 at 14:12
  • but both of them give the same result – Deep Roy Dec 01 '17 at 14:14
  • no, second should not wok as you should not create instance of Activity derrived class on android platform ... **also in first case you are passing the outer class instance as parameter and in second case you are passing new instance of the same class** ... you should learn some java's basic before starting android programming – Selvin Dec 01 '17 at 14:15
  • very sorry ...It does not work...I got it...but both are returning objects of the outer class. The difference is that one is making an entirely new object but the other is referencing the current Outer class instance...please correct me If I am wrong. – Deep Roy Dec 01 '17 at 14:56

2 Answers2

1

Those two operators are not comparable.

While using new MainActivity(), you are creating in memory a new object, identified as a MainActivity.

Documentation for "new".

While using MainActivity.this, you are refering to the object where you are, from the MainActivity class. It only works because you are already in a MainActivity class. Your compiler understands this, and MainActivity refers to itself. (Take me back if I'm wrong, I'm not sure about this whole textblock).

Documentation for "this".

I would advise you using the first usage. The second one is tricky and you would not understand why it does not work, confusing you in your understanding of Java and (in a sense) object-oriented programming.

Yassine Badache
  • 1,810
  • 1
  • 19
  • 38
  • but actually, both are filling the place for the instance of the Outer class isn't? One is creating simply an entirely new object but the other is referencing and returning the current object...Please tell me If I'm thinking wrong? – Deep Roy Dec 01 '17 at 14:46
  • Both are filling the place for the instance of the Outer class. The part about my advice is only my opinion, as Andy on another answer I'm no expert either. I would advise you checking about a guide on `Intent` on Android, or simply learn "plain" Java before going in those kind of cases. – Yassine Badache Dec 01 '17 at 14:59
  • Sir, I appreciate your advice...can you please help me by suggesting any resources about how to learn about efficiently about inner class, local classes, anonymous classes and other nested concepts...the oracle docs seem to very very overwhelming...Thanks a lot – Deep Roy Dec 01 '17 at 15:06
  • There are a lot. When in doubt, the [Oracle official documentation](https://docs.oracle.com/javase/tutorial/java/index.html) is always helpful. If you like interacting coding, the [Codecademy](https://www.codecademy.com/learn/learn-java/) has some nice tutorials, in the form of assisted projects. [Java Lessons](http://javalessons.com) will teach you from examples (this is one of the best way), and if you feel ready to try things out in Java later on, I advise you to try some [Code katas](http://codekata.com/). And, of course, when in doubt, check first here on StackOverflow (good reflex :)). – Yassine Badache Dec 01 '17 at 15:11
  • 1
    It has been a great help...I appreciate it a lot – Deep Roy Dec 01 '17 at 15:13
  • public class MyClass { int x = 0; public static void main(String args[]) { MyClass i = new MyClass(); i.x=78; i.print(); } public void print(){ System.out.println(new MyClass().x); System.out.print(this.x); } } This code gives the result as: 0 78 Probably it's self explanatory – Deep Roy Dec 01 '17 at 17:12
  • Glad I helped. Be well on your coding journey. – Yassine Badache Dec 01 '17 at 20:02
0
  1. new creates a new instance (object), so new MainActivity() creates a new instance of MainActivity.

  2. MainActivity.this refers to the already existing instance of MainActivity. This format is necessary, because you are accessing it from within the instance of View.OnClickListener where a plain this would reference this instance of View.OnClickListener.

So, I don't know Android that much, but I think what you want to do is to go with MainActivity.this because it refernces the already existing object of MainActivity.

Andy
  • 1,964
  • 1
  • 15
  • 29