class with all public functions as static
- User
class with all public functions as static
- Assistant
class with all public functions as static
Creating a class where all public functions are static can be useful for utility or helper classes that provide functionality without needing to maintain any internal state. Static methods belong to the class itself rather than to any particular instance, meaning you can call them without creating an object of the class.
In Java, utility classes often have a private constructor to prevent instantiation.
public class MathUtils {
// Private constructor to prevent instantiation
private MathUtils() {
throw new UnsupportedOperationException("Utility class cannot be instantiated");
}
public static int add(int a, int b) {
return a + b;
}
public static double sqrt(double value) {
return Math.sqrt(value);
}
// Add more static utility methods as needed
}
Usage:
public class Main {
public static void main(String[] args) {
int sum = MathUtils.add(5, 10);
double root = MathUtils.sqrt(16.0);
System.out.println("Sum: " + sum); // Output: Sum: 15
System.out.println("Square Root: " + root); // Output: Square Root: 4.0
}
}
In C#, similar practices apply. You can use the static keyword on the class itself to ensure all members are static.
public static class StringUtils
{
public static bool IsNullOrEmpty(string str)
{
return string.IsNullOrEmpty(str);
}
public static string ToUpperCase(string str)
{
return str.ToUpper();
}
// Add more static utility methods as needed
}
Usage:
public class Program
{
public static void Main(string[] args)
{
string test = "hello";
bool isEmpty = StringUtils.IsNullOrEmpty(test);
string upper = StringUtils.ToUpperCase(test);
Console.WriteLine($"Is Empty: {isEmpty}"); // Output: Is Empty: False
Console.WriteLine($"Uppercase: {upper}"); // Output: Uppercase: HELLO
}
}
In C++, you can create a class with all static member functions. However, C++ does not have a static class keyword, so you need to make sure not to instantiate the class.
#include <iostream>
#include <cmath>
#include <string>
class Utility {
public:
// Delete the constructor to prevent instantiation
Utility() = delete;
static int add(int a, int b) {
return a + b;
}
static double sqrtFunc(double value) {
return std::sqrt(value);
}
static std::string toUpperCase(const std::string& str) {
std::string upper = str;
for (auto& c : upper) c = toupper(c);
return upper;
}
// Add more static utility methods as needed
};
int main() {
int sum = Utility::add(5, 10);
double root = Utility::sqrtFunc(16.0);
std::string upper = Utility::toUpperCase("hello");
std::cout << "Sum: " << sum << std::endl; // Output: Sum: 15
std::cout << "Square Root: " << root << std::endl; // Output: Square Root: 4
std::cout << "Uppercase: " << upper << std::endl; // Output: Uppercase: HELLO
return 0;
}
static (C#) to prevent creating instances.Using a class with all public functions as static is a common pattern for utility classes that provide stateless functionality. It can lead to cleaner and more organized code when used appropriately. However, be mindful of the limitations regarding inheritance, polymorphism, and testing.
If you have a specific language or use case in mind, feel free to provide more details for a tailored example!
Not yet included in a published release.