HyunJong Yui
HyunJong Yui

Reputation: 57

Which way is better using static on android

I developing an android application. I'm confused between two way to use static for customized UI component like dialog, progress bar or alert.

See below.

public class UiUtils {

    public static void inputAlertDialogShow(Context context, final View view ,String message,DialogInterface.OnClickListener listener)
   {

    CustomDialog.Builder customBuilder = new
            CustomDialog.Builder(context);

    if(listener!=null) {
        customBuilder.setMessage(message).setPositiveButton(context.getResources().getString(R.string.dialog_confirm), listener);
    }else{
        customBuilder.setMessage(message).setPositiveButton(context.getResources().getString(R.string.dialog_confirm),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (view != null) view.requestFocus();
                    }
                });
    }
    customBuilder.create().show();
  }
}

I made method showing dialog as static method. So I can call customized dialog to anywhere like

UiUtils.inputAlertDialogShow(context, view, message, listener).

But I can also use it like this

UiUtils ui = new UiUtils();
ui.inputAlertDialogShow(context, view, message, listener);

Can anyone explain me which one is better and why?

Upvotes: 0

Views: 115

Answers (5)

RBK
RBK

Reputation: 2417

you can use both method. because compiler will change it to static call like

Class.methodToCall();

refer this SO answer

and according to java practice, it recommend to call without object.

Upvotes: 0

kreed
kreed

Reputation: 46

There is just one thing you need to consider, are you writing testable code? If you want your code base to be testable then, static methods is not the way to go, because in order to test static methods of a class you need to use PowerMock, the basic Mockito library can't mock static methods. so in order to make the code base testable you need to not use static methods and use dependency injection to inject the UiUtils class to whichever class depends on the UiUtils

So if you want to know theoretically which one is better? Then not using static methods is better, because it makes for testable code.

Upvotes: 0

Haniyeh Khaksar
Haniyeh Khaksar

Reputation: 804

you can't do it. when you define a variable as static, you can't make an instance to access it. static is the best way to access objects and method without make an instance. why you try to make an instance? do you know by making an instance, you reserve some memory?

Upvotes: 0

Rahul
Rahul

Reputation: 510

UiUtils.inputAlertDialogShow(context, view, message, listener).

Call directly like above and one more thing and keep callback with you when you call this function from any activity or fragment.

Hope it will help you !

Upvotes: 1

Mustansar Saeed
Mustansar Saeed

Reputation: 2790

Utilities don't need to be instantiated. Utility class purpose is to provide the commonly used functions that's why all functions in Utility class are made static and they are called Utility.functionName so its better to call UiUtils.inputAlertDialogShow(context, view, message, listener).

Hope this helps.

Upvotes: 1

Related Questions