Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8302885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:23:48+00:00 2026-06-08T17:23:48+00:00

This is may be a noob question, but I was wondering why do we

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:23:48+00:00Added an answer on June 8, 2026 at 5:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may sound like a noob question, because it is. I have tried for
This may be a stupid question but I have a code with the following
This may sound like a very generic question but here it goes. I have
This may be a stupid question but its not a matter of what I
This may be a silly question (but I'm new to multicast). I have a
It may be a noob question but is this something to do with my
Ok this may seem like a bit of a noob question but one many
This may be a noob question but I want to hide the content within
this may sound like a strange question from a Python noob, but here's the
I know this may be a noob question, but it's bugging the heck out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.