4
I have some methods that I use in various points of the applications and I end up having to copy in each class.
For example:
String getStringOrEmpty(EditText editText) {
String mString = editText.getText().toString();
mString = (mString.isEmpty() ? "" : mString);
return mString;
}
Just to inform: this method returns a String with the text that Edittext, if any, or an empty String.
I didn’t want to have to keep copying it in every class that will use it, but I would like to access this and other methods throughout the project.
This would be similar to a function library in other languages. For example, in php, I would make a file with a list of functions and use a include command to import the list to the page.
How to do this in Java (with emphasis on Android)?
If you need to use in several classes, of the two one, either it does not belong to these classes but to another that has a specific responsibility, or it belongs to a mother class that will be used to derive for the daughters that need it and but it seems to be more this last case, but I can’t say with just what was put in the question.
– Maniero
Actually, three times one: ... or you put him in a Mixin class, and use multiple inheritance. The problem is that the language in question does not support multiple inheritance - so you have to force the business to be arranged differently - possibly with an artificial base class - anyway, the question is very pertinent.
– jsbueno
E.. in fact, in addition to multiple inheritances, something else that would do well would be able to have functions - without relying on an artificial "static class". I hope that all the karma of the people who chose to use Java for Android will bring some benefit to their souls in the future.
– jsbueno
I understood the comments, but the solution has to be within the limits of the language. The solution of a class
utils
with static methods seems to me the most suitable for the desired purpose. Both for ease and resource saving, since the methods are instantiated only once.– Rene Freak