Static methods
We know quite a lot about classes already. For example, we know how to turn them into objects and use their methods and variables. But something isn't quite right. Since the very start of the book, we have been using two classes more than any other. We have repeatedly used Log
and Toast
to output to logcat or the user's screen but have not instantiated them once! How can this be? We never did this:
Log myLog = new Log(); Toast myToast = new Toast();
We just went ahead and used the classes directly, like this:
Log.i("info","our message here"); Toast.makeText(this, "our message", Toast.LENGTH_SHORT).show();
The static methods of classes can be used without first instantiating an object of the class. We can think of this as a static method belonging to the class and all other methods belonging to an object/instance of a class.
And as you have probably realized by now,...