Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static methods on generic classes?

Okay, this is the case:

I got a generic base-class which I need to initialize with some static values. These values have nothing to do with the kind of types my generic baseclass is loaded with.

I want to be able to do something like this:

GenericBaseclass.Initialize(AssociatedObject);

while also having a class doing like this:

public class DerivedClass : GenericBaseclass<int>
{
   ...
}

Is there any way to accomplish this? I could make a non-generic baseclass and put the static method there, but I don't like that "hack" :)

like image 942
cwap Avatar asked Mar 31 '09 20:03

cwap


People also ask

Can generic class have static method?

1 Answer. You can't use a class's generic type parameters in static methods or static fields. The class's type parameters are only in scope for instance methods and instance fields.

Why generic methods are static?

This is because, the static method can be called without even instantiating the Class. So if the Class is not yet instantiated, we do not yet know what is T. This is the reason why the static methods needs to have its own generics.

Are generics static in Java?

Using generics, type parameters are not allowed to be static. As static variable is shared among object so compiler can not determine which type to used. Consider the following example if static type parameters were allowed.

What are static methods in class?

What is a static method? Static methods, much like class methods, are methods that are bound to a class rather than its object. They do not require a class instance creation. So, they are not dependent on the state of the object.


1 Answers

If the values have nothing to do with the type of the generic base class, then they shouldn't be in the generic base class. They should either be in a completely separate class, or in a non-generic base class of the generic class.

Bear in mind that for static variables, you get a different static variable per type argument combination:

using System;

public class GenericType<TFirst, TSecond>
{
    // Never use a public mutable field normally, of course.
    public static string Foo;
}

public class Test
{    
    static void Main()
    {
        // Assign to different combination
        GenericType<string,int>.Foo = "string,int";
        GenericType<int,Guid>.Foo = "int,Guid";
        GenericType<int,int>.Foo = "int,int";
        GenericType<string,string>.Foo = "string,string";


        // Verify that they really are different variables
        Console.WriteLine(GenericType<string,int>.Foo);
        Console.WriteLine(GenericType<int,Guid>.Foo);
        Console.WriteLine(GenericType<int,int>.Foo);
        Console.WriteLine(GenericType<string,string>.Foo);

    }
}

It sounds like you don't really want a different static variable per T of your generic base class - so you can't have it in your generic base class.

like image 96
Jon Skeet Avatar answered Oct 07 '22 21:10

Jon Skeet