What is a “static class” in Java?

Java has static nested classes but it sounds like you’re looking for a top-level static class. Java has no way of making a top-level class static but you can simulate a static class like this: Declare your class final – Prevents extension of the class since extending a static class makes no sense Make the … Read more

Why does Android prefer static classes

It’s not just Android developers… A non-static inner class always keeps an implicit reference to the enclosing object. If you don’t need that reference, all it does is cost memory. Consider this: class Outer { class NonStaticInner {} static class StaticInner {} public List<Object> foo(){ return Arrays.asList( new NonStaticInner(), new StaticInner()); } } When you … Read more

How to pass parameter to static class constructor?

Don’t use a static constructor, but a static initialization method: public class A { private static string ParamA { get; set; } public static void Init(string paramA) { ParamA = paramA; } } In C#, static constructors are parameterless, and there’re few approaches to overcome this limitation. One is what I’ve suggested you above.

What is a “static” class?

Firstly, a comment on an answer asked about what “static” means. In C# terms, “static” means “relating to the type itself, rather than an instance of the type.” You access a static member (from another type) using the type name instead of a reference or a value. For example: // Static method, so called using … Read more

ASP.NET Core Web API Logging from a Static Class

Solution is to have a static reference to the LoggerFactory in a utility static class initialized on startup: /// <summary> /// Shared logger /// </summary> internal static class ApplicationLogging { internal static ILoggerFactory LoggerFactory { get; set; }// = new LoggerFactory(); internal static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>(); internal static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName); } … Read more

C# Static class vs struct for predefined strings

With the struct solution, there’s nothing to stop some other code doing new PredefinedStrings(), which won’t do anything bad, but is something it’s semantically confusing to allow. With a static class the compiler will forbid creation for you. And it goes without saying that static class is the preferred way of providing constants in the … Read more