0

I had seen that the Toast in android->

 Toast toast = Toast.makeText(context, text, duration);
toast.show();

From seen that, I think that makeText a static method and show method non-static method.

But after that seen->

Toast.makeText(context, text, duration).show();

I am get confused. How show() is called without creating an object of Toast class?(If show method is non-static-according to first code)

  • `You can accept one answer (if it helps you) by click on big gray check button on its left side. If you wish you can add +10 points to any author of any good answer by click upper gray triangle` – MaLa Dec 26 '20 at 14:19
  • Yeah I know that. But i have no sufficient reputation for giving feedbacks. – Arghadeep dey Dec 31 '20 at 13:39

2 Answers2

2

The two pieces of code you show are the same.

The second one calls Toast.makeText(context, text, duration), takes the return from that (the Toast instance) and calls show() on it.

The first code block makes a local variable called toast but the behavior is the same in both.

Brian Walker
  • 8,658
  • 2
  • 33
  • 35
1

Show() is not a static method. Show() can be called because MakeText() returns a Toast object.

MaLa
  • 582
  • 2
  • 16
  • I have question about that-> 1. How to create a object without using "new" keyword(First explanation)? 2. It's confirm that makeText method is static otherwise I am not calling a method without creating an object. In addition that my next question is -> then how to call show method without creating an object(Second explanation)? In addition I am new in java. If you have any refence doc regarding this then it's very easy for me. – Arghadeep dey Dec 10 '20 at 09:26
  • 1. [Have you checked this](https://stackoverflow.com/questions/16607261/instantiate-objects-without-using-new-operator) 2. MakeText() creates Toast object. Because of that you can call `Toast.makeText().show()`. It seems like that you are not familiar with static methods. I would advice you to read about static methods or look up a tutorial. – MaLa Dec 10 '20 at 11:56