Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Toast.makeText and not new Toast

This is may be a noob question, but I was wondering why do we have to use a static method (makeText) to create a Toast and not a constructor.

Why do we have to use this:

makeText(Context context, CharSequence text, int duration)

instead of this:

new Toast(Context context, CharSequence text, int duration) 

This is the makeText method:

    public static Toast makeText(Context context, CharSequence text, int duration) {
        Toast result = new Toast(context);

        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
        TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
        tv.setText(text);

        result.mNextView = v;
        result.mDuration = duration;

        return result;
    }

Why don't we have the following:

public Toast (Context context, CharSequence text, int duration) {
    this(context);

    LayoutInflater inflate = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
    TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
    tv.setText(text);

    this.mNextView = v;
    this.mDuration = duration;
}

I searched the web and source code for any reason but I didn't find.

Please if you have an idea, don't hesitate.

like image 499
AMerle Avatar asked Jul 30 '12 08:07

AMerle


1 Answers

The question basically drill downs to when should I make a method static. The answer is simple- when your method has a very specific task and does not change the state of the object.

Something like a utility method, say add(int a, int b) which simply returns a+b. If I need to store the value a+b for later use for the object, static method is strictly no-no (you will not be able to store a non static variable in a static method). But if we are dealing with some action which is independent of state of the object, static is the answer.

Why are we giving preference to static if it is independent of state of object?

  1. Memory- static method will only have one copy, irrespective of the actual number of object.

  2. Availability- Method is available even if you don't have single object

Ofcourse the downside is that we are keeping a copy of method even if we do not use it at all (if it was non-static and no object was created, we would have saved this space). But this is of lower weight than the advantages mentioned above.

As the method we are discussing here (makeText), does not need to maintain a state for later use, the best way to go is static method.

--Edit--

The answer mentioned above is more generic as to when we should use static and when non-static, let me get specific to Toast class.

Toast class gives us 2 ways to create a Toast object (Refer http://developer.android.com/reference/android/widget/Toast.html)

  1. makeText(Context context, CharSequence text, int duration) which returns a Toast object which the values assigned.

  2. Normal way, use new Toast(context) to create an object, then set values as required.

If you use method 1, you are saying something like Toast.makeText(context, text, duration).show(); and we are done. I use this method all the time.

Method 2 is used only for a specific case, from http://developer.android.com/guide/topics/ui/notifiers/toasts.html

Do not use the public constructor for a Toast unless you are going to define the layout with setView(View). If you do not have a custom layout to use, you must use makeText(Context, int, int) to create the Toast.

@CFlex, If I got your question properly, I guess you just want to know why we have Toast.makeText(context, text, duration) returning a Toast object, when the same thing could have been done by a constructor.

Whenever I look at something like ClassName.getObject returning object of class, I think about singleton pattern. Well, in this case we are not exactly talking about singleton, I would like to assume that makeText returns same object always (to save creation of N objects), otherwise it is just a fancy thing developed by Android team.

like image 61
Kamal Avatar answered Sep 21 '22 00:09

Kamal