For questions regarding utility class methods.
Questions tagged [utility-method]
69 questions
78
votes
4 answers
How can I create an utility class?
I want to create a class with utility methods, for example
public class Util {
public static void f (int i) {...}
public static int g (int i, int j) {...}
}
Which is the best method to create an utility class?
Should I use a private…

user2693979
- 2,482
- 4
- 24
- 23
75
votes
3 answers
If a "Utilities" class is evil, where do I put my generic code?
I generally live by the rule that Global variables / functions are evil and that every piece of code should live in the class to which it pertains.
This is a very easy rule to follow, and I believe that I haven't ever run into an issue with this…

John Gietzen
- 48,783
- 32
- 145
- 190
44
votes
2 answers
Where to put utility functions in a React-Redux application?
In a React-Redux application, I've got a state like this:
state = {
items: [
{ id: 1, first_name: "John", last_name: "Smith", country: "us" },
{ id: 2, first_name: "Jinping", last_name: "Xi", country: "cn" },
]
}
which I…

laurent
- 88,262
- 77
- 290
- 428
44
votes
6 answers
Utility method to convert Boolean into boolean and handle null in Java
Is there a utility method in Java which converts Boolean into boolean and automatically handles null reference to Boolean as false?

ps-aux
- 11,627
- 25
- 81
- 128
28
votes
4 answers
Are interfaces a valid substitute for utility classes in Java 8?
For the past decade or so, I've been using the pattern below for my Java utility classes. The class contains only static methods and fields, is declared final so it can't be extended, and has a private constructor so it can't be instantiated.
public…

Robby Cornelissen
- 91,784
- 22
- 134
- 156
17
votes
1 answer
Is it possible to pass class as a parameter in a function in Flutter?
Here, I have a utility class in which I have a function for showing DialogBox, so I'm trying to make an AlertDialog Box which can be used anywhere in the whole project.
So, I have to pass Title, description as an argument and also want to pass Class…

guruprakash gupta
- 877
- 4
- 9
- 15
14
votes
7 answers
Find common parent-path in list of files and directories
I got a list of files and directories List pathes. Now I'd like to calculate the deepest common branch every path is sharing with each other.
We can assume that they all share a common path, but this is unknown in the beginning.
Let's say I…

Frame91
- 3,670
- 8
- 45
- 89
11
votes
9 answers
Utility method - Pass a File or String?
Here's an example of a utility method:
public static Long getFileSize(String fileString) {
File file = new File(fileString);
if (file == null || !file.isFile())
return null;
return file.length();
}
Is it a good practise to…

James P.
- 19,313
- 27
- 97
- 155
10
votes
4 answers
Where shall I put my utilities classes in a ASP.NET MVC3 application?
I am developing a web application in ASP.NET MVC3 with C# and Razor.
I need to create an utility class where I put functions to convert string into dates(years, months, days, etc...).
In ASP.NET Web Forms I used to place this kind of classes inside…

CiccioMiami
- 8,028
- 32
- 90
- 151
9
votes
1 answer
Passing reference to activity to utility class android
I realise this question has been asked many times, but I am still unable to completely understand this concept. In my application, I am using a static utility class to keep common methods (like showing error dialogs)
Here is how my static class…

TMS
- 1,201
- 1
- 13
- 20
8
votes
4 answers
Get File Extension for special cases like tar.gz
I need to extract extensions from file names.
I know this can be done for single extensions like .gz or .tar by using filePath.lastIndexOf('.') or using utility methods like FilenameUtils.getExtension(filePath) from Apache commons-io.
But, what if I…

Bernice
- 2,552
- 11
- 42
- 74
7
votes
2 answers
Best practices for JQuery namespaces + general purpose utility functions
What are some current "rules of thumb" for implementing JQuery namespaces to host general purpose utility functions?
I have a number of JavaScript utility methods scattered in various files that I'd like to consolidate into one (or more) namespaces.…

Armchair Bronco
- 2,367
- 4
- 31
- 44
6
votes
1 answer
How Make iPhone to silent mode on/off using objective c
I am creating one app in which i want to detect that iphone is on silent mode or not.
I have already gone thought the below link
Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?
Switching between silent mode and…

Crazy Developer
- 3,464
- 3
- 28
- 62
6
votes
7 answers
how to pass parameter to static initialize block
What I want to do is load key/value pairs from a file (excel file using Apache poi) into a static map that will be used as a lookup table. Once loaded the table will not change.
public final class LookupTable
{
private final static Map

cvue
- 229
- 2
- 3
- 6
6
votes
3 answers
Utility class that re-throws a throwable as unchecked?
I am catching all throwables in a unit test wrapper method in order to reset some data in an external system. I want to re-throw the original exception when this is done and I am using this piece of code to do it:
if (t instanceof RuntimeException)…

billc.cn
- 7,187
- 3
- 39
- 79