0

I accidentally coded my Toast this way and it continues to work.

public class MainActivity extends AppCompatActivity  {

  private Toast mToast;

  ...

  private void onClickHandler(int itemIndex) {
    mToast = new Toast();
    mToast.makeText(this, String.valueOf(itemIndex), Toast.LENGTH_SHORT).show();
  }
}

Given that makeText is a static method, why would the above work?

Boon
  • 40,656
  • 60
  • 209
  • 315

1 Answers1

2

Because static methods and variables can also by accessed from an instance of the class

However, accessing them that way is considered bad practice

Larpee
  • 800
  • 1
  • 7
  • 18
  • 1
    Yep, that and Android Studio is one of the IDEs that highlights it as a suggested change as opposed to a compile error when using Java. Better yet, Kotlin doesn't tolerate using static methods from instances at all. – DaveNOTDavid Jun 18 '17 at 01:46