0

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?

yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42

5 Answers5

1

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.

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46
  • Its not related to memory. Its about the requirement what you want to achieve. As in case of Utility functions, you don't need an instance of utility class, in this case it will consume memory un-necessarily because most commonly functions can be made static and you don't need instance hence saving memory . Hope it clears – Mustansar Saeed Feb 24 '16 at 05:30
  • Is there any bad effect on device's memory making those method static? – HyunJong Yui Feb 24 '16 at 05:31
1
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 !

Rahul
  • 510
  • 3
  • 8
0

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?

Haniyeh Khaksar
  • 774
  • 4
  • 20
0

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.

kreed
  • 46
  • 1
0

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.

Community
  • 1
  • 1
RBK
  • 2,481
  • 2
  • 30
  • 52